...
Code Block | ||||
---|---|---|---|---|
| ||||
// get content type service from repository $contentTypeService = $repository->getContentTypeService(); // load the content type group try { $contentTypeGroup = $contentTypeService->loadContentTypeGroupByIdentifier($groupIdentifier); } catch(\eZ\Publish\API\Repository\Exceptions\NotFoundException $e) { $output->writeln("content type group with identifier $groupIdentifier not found"); return; } // instanciate a ContentTypeCreateStruct with the given content type identifier $contentTypeCreateStruct = $contentTypeService->newContentTypeCreateStruct($contentTypeIdentifier ); // the main language code for names and description $contentTypeCreateStruct->mainLanguageCode = 'eng-GB'; // the name schema for generating the content name by using the title attribute $contentTypeCreateStruct->nameSchema = '<title>'; // set names for the content type $contentTypeCreateStruct->names = array( 'eng-GB' => $contentTypeIdentifier . 'eng-GB', 'ger-DE' => $contentTypeIdentifier . 'ger-DE', ); // set description for the content type $contentTypeCreateStruct->descriptions = array( 'eng-GB' => 'Description for ' . $contentTypeIdentifier . 'eng-GB', 'ger-DE' => 'Description for ' . $contentTypeIdentifier . 'ger-DE', ); /********************** add fields ***************************************/ // add a title field $titleFieldCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct('title', 'ezstring'); // set names and description for display $titleFieldCreateStruct->names = array('eng-GB' => 'Title','ger-DE' => 'Titel',); $titleFieldCreateStruct->descriptions = array('eng-GB' => 'The Title','ger-DE' => 'Der Titel'); // set an group for the field $titleFieldCreateStruct->fieldGroup = 'content'; // set position inside the content type $titleFieldCreateStruct->position = 1; // enable translation $titleFieldCreateStruct->isTranslatable = true; // require this field to set on content creation $titleFieldCreateStruct->isRequired = true; // enabled to find field via content search $titleFieldCreateStruct->isSearchable = true; // add field definition to content create struct $contentTypeCreateStruct->addFieldDefinition( $titleFieldCreateStruct ); // add a body field $bodyFieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('body', 'ezstring'); // set names and description for display $bodyFieldCreate->names = array('eng-GB' => 'Body','ger-DE' => 'Text'); $bodyFieldCreate->descriptions = array('eng-GB' => 'Description for Body','ger-DE' => 'Beschreibung Text'); $bodyFieldCreate->fieldGroup = 'content'; $bodyFieldCreate->position = 2; $bodyFieldCreate->isTranslatable = true; $bodyFieldCreate->isRequired = true; $bodyFieldCreate->isSearchable = true; // add field definition to content create struct $contentTypeCreateStruct->addFieldDefinition( $bodyFieldCreate ); // set the content type group for the content type $groups = array($contentTypeGroup); // start a transaction $repository->beginTransaction(); try { // create the content type - the returned content type is in status DRAFT $contentTypeDraft = $contentTypeService->createContentType($contentTypeCreateStruct,$groups); // publish the content type draft $contentTypeService->publishContentTypeDraft($contentTypeDraft); // commit the transaction $repository->commit(); } catch( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e ) { // react on permission denied $output->writeln($e->getMessage()); $repository->rollback(); } catch( \eZ\Publish\API\Repository\Exceptions\ForbiddenException $e ) { // react on identifier already exists $output->writeln($e->getMessage()); } catch( \Exception $e ) { $output->writeln($e->getMessage()); $repository->rollback(); } } |
Receipt 5 - Creating content
In this receipt content is created under a given parent location. It is assumed that the loaded content type is the one created in receipt 4.
Code Block | ||||
---|---|---|---|---|
| ||||
// get the content service from the repository
$contentService = $repository->getContentService();
// get the location service from the repsitory
$locationService = $repository->getLocationService();
// get the user service from the repsitory
$contentTypeService = $repository->getContentTypeService();
try
{
// load the content type with identifier
$contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
// instanciate a location create struct
$locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId);
// instanciate a content creation struct
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
// set title field
$contentCreateStruct->setField('title',$title);
// set body field
$contentCreateStruct->setField('body', $body);
// create a draft using the content and location create structs
$draft = $contentService->createContent($contentCreateStruct, array($locationCreateStruct));
// publish the content draft
$content = $contentService->publishVersion($draft->versionInfo);
// print out the content
print_r($content);
}
catch(\eZ\Publish\API\Repository\Exceptions\NotFoundException $e)
{
// react on content type or location not found
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException $e)
{
// react on remote id exists already
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $e)
{
// react on a field is not valid
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\ContentValidationException $e)
{
// react on a required field is missing or empty
$output->writeln($e->getMessage());
}
} |
Receipt 6 - Updating Content
In this receipt the previously created content is updated with a new title and body in the same language
Code Block | ||||
---|---|---|---|---|
| ||||
try
{
// load the content info for the given id
$contentInfo = $contentService->loadContentInfo($contentId);
// create a draft from the current published version
$contentDraft = $contentService->createContentDraft($contentInfo);
// instanciate a content update struct
$contentUpdateStruct = $contentService->newContentUpdateStruct();
// set language for new version
$contentUpdateStruct->initialLanguageCode = 'eng-GB';
// set fields
$contentUpdateStruct->setField( 'title', $newtitle );
$contentUpdateStruct->setField( 'body', $newbody );
// update draft
$contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
// publish draft
$content = $contentService->publishVersion($contentDraft->versionInfo);
print_r($content);
}
catch(\eZ\Publish\API\Repository\Exceptions\NotFoundException $e)
{
// react on content type not found
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $e)
{
// react on a field is not valid
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\ContentValidationException $e)
{
// react on a required field is missing or empty
$output->writeln($e->getMessage());
}
} |
Receipt 7 - Translating content
It is the same code as for updating (see Receipt 6). The initial language should be set to the translation language.
Code Block | ||||
---|---|---|---|---|
| ||||
// set language for new version
$contentUpdateStruct->initialLanguageCode = $newLanguage;
// set fields
$contentUpdateStruct->setField( 'title', $newtitle );
$contentUpdateStruct->setField( 'body', $newbody );
|
Receipt 8 - Multiple translations at once
It is possible to to make an update in content with more than one language. But there is a restriction - only one language can be assigned to the newly created version (which is displayed in the 4.x admin GUI in the translations column).
Code Block | ||||
---|---|---|---|---|
| ||||
// set one language for new version
$contentUpdateStruct->initialLanguageCode = 'fra-FR';
// set fields for german - here the language has to be passed in third argument
$contentUpdateStruct->setField( 'title', $newgermantitle, 'ger-DE' );
$contentUpdateStruct->setField( 'body', $newgermanbody, 'ger-DE' );
// set fields for french
$contentUpdateStruct->setField( 'title', $newfrenchtitle);
$contentUpdateStruct->setField( 'body', $newfrenchbody);
|