...
php app/console cookbook:run 57
Receipt
...
2 - Creating a content type group
This snippet creates a content type group for a given identifier.
...
If this snipped is run with the same init code from receipt 1 we will get an UnauthorizedException.
The solution is described in the next receipt.
...
$userService->loadUser($id)
Receipt 4 - Creating a content type
With this snipped a content type with two fields of type 'ezstring' is created.
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();
}
} |