...
php app/console cookbook:run 5
Receipt 2 - Creating a content type group
...
Code Block |
---|
language | php |
---|
title | create content type group |
---|
linenumbers | true |
---|
|
$contentTypeService = $repository->getContentTypeService();
try
{
// instanciate a create struct
$groupCreate$contentTypeGroupCreateStruct = $this->contentTypeService->newContentTypeGroupCreateStruct($groupIdentifier);
// call service method
$contentTypeGroup = $this->contentTypeService->createContentTypeGroup( $groupCreate$contentTypeGroupCreateStruct );
// print out the group
print_r($contentTypeGroup);
}
catch( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e )
{
// react on permission denied
$output->writeln($e->getMessage());
}
catch( \eZ\Publish\API\Repository\Exceptions\ForbiddenException $e )
{
// react on identifier already exists
$output->writeln($e->getMessage());
} |
If this snipped is run with the same init code from receipt 1 we will get an UnauthorizedException.
...
$userService->loadUser($id)
Receipt 4 - Creating a content type
...
Code Block |
---|
language | php |
---|
title | create content type |
---|
|
// 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$titleFieldDefinitionCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct('title', 'ezstring');
// set names and description for display
$titleFieldCreateStruct$titleFieldDefinitionCreateStruct->names = array('eng-GB' => 'Title','ger-DE' => 'Titel',);
$titleFieldCreateStruct$titleFieldDefinitionCreateStruct->descriptions = array('eng-GB' => 'The Title','ger-DE' => 'Der Titel');
// set an group for the field
$titleFieldCreateStruct$titleFieldDefinitionCreateStruct->fieldGroup = 'content';
// set position inside the content type
$titleFieldCreateStruct$titleFieldDefinitionCreateStruct->position = 1;
// enable translation
$titleFieldCreateStruct$titleFieldDefinitionCreateStruct->isTranslatable = true;
// require this field to set on content creation
$titleFieldCreateStruct$titleFieldDefinitionCreateStruct->isRequired = true;
// enabled to find field via content search
$titleFieldCreateStruct$titleFieldDefinitionCreateStruct->isSearchable = true;
// add field definition to content create struct
$contentTypeCreateStruct->addFieldDefinition( $titleFieldCreateStruct$titleFieldDefinitionCreateStruct );
// add a body field
$bodyFieldCreate$bodyFieldDefinitionCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct('body', 'ezstring');
// set names and description for display
$bodyFieldCreate$bodyFieldDefinitionCreateStruct->names = array('eng-GB' => 'Body','ger-DE' => 'Text');
$bodyFieldCreate$bodyFieldDefinitionCreateStruct->descriptions = array('eng-GB' => 'Description for Body','ger-DE' => 'Beschreibung Text');
$bodyFieldCreate$bodyFieldDefinitionCreateStruct->fieldGroup = 'content';
$bodyFieldCreate$bodyFieldDefinitionCreateStruct->position = 2;
$bodyFieldCreate$bodyFieldDefinitionCreateStruct->isTranslatable = true;
$bodyFieldCreate$bodyFieldDefinitionCreateStruct->isRequired = true;
$bodyFieldCreate$bodyFieldDefinitionCreateStruct->isSearchable = true;
// add field definition to content create struct
$contentTypeCreateStruct->addFieldDefinition( $bodyFieldCreate$bodyFieldDefinitionCreateStruct );
// set the content type group for the content type
$groups$contentTypeGroups = 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$contentTypeGroups);
// 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());
$repository->rollback();
}
catch( \Exception $e )
{
$output->writeln($e->getMessage());
$repository->rollback();
}
} |
Receipt 5 - Creating content
...
Code Block |
---|
language | php |
---|
title | create content |
---|
|
// 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$draftContent = $contentService->createContent($contentCreateStruct, array($locationCreateStruct));
// publish the content draft
$content = $contentService->publishVersion($draft$draftContent->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());
}
catch( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e )
{
// react on permission denied
$output->writeln($e->getMessage());
}
} |
Receipt 6 - Updating Content
...
Code Block |
---|
language | php |
---|
title | update content |
---|
|
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 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
...
Code Block |
---|
language | php |
---|
title | a simple full text search |
---|
|
// get the search service
$searchService = $repository->getSearchService();
// create a new query object
$query = new \eZ\Publish\API\Repository\Values\Content\Query();
// add a fulltext criterion
$query->criterion = new \eZ\Publish\API\Repository\Values\Content\Query\Criterion\Fulltext($text);
// call findContent
$result = $searchService->findContent($query);
// print the total count of the search hits
$output->writeln('Found ' . $result->totalCount . ' items');
// iterate over the search hits
foreach( $result->searchHits as $searchHit ) {
// print out the content name
$output->writeln($searchHit->valueObject->contentInfo->name);
} |
Receipt 10 - Performing an advanced search
In this receipt different criteria is combined using a logic and operation. The result is restricted additional (see Receipt 9) to a given content type and subtree.
...
Code Block |
---|
language | php |
---|
title | The method browseLocation |
---|
|
/**
* this method prints out the location name and calls this method recursive for the locations children
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
* @param int $depth the current depth
* @param OutputInterface $output
*/
private function browseLocation(Location $location, $depth, OutputInterface $output) {
// indent according to depth
for($k=0;$k<$depth;$k++)
{
$output->write(' ');
}
// write the content name
$output->writeln($location->contentInfo->name);
// get location children
$children = $this->locationService->loadLocationChildren($location);
// browse children
foreach($children as $childLocation)
{
$this->browseLocation($childLocation, $depth +1,$output);
}
} |
Receipt 12 - Adding a new location to content
...
Code Block |
---|
language | php |
---|
title | add location |
---|
|
// get the location service from the repsitory
$locationService = $repository->getLocationService();
// get the user service from the repsitory
$userService = $repository->getUserService();
// load admin user
$user = $userService->loadUser(14);
// set current user to admin
$repository->setCurrentUser($user);
try
{
// instanciate a location create struct
$locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId);
// load the content info from the given content id
$contentInfo = $contentService->loadContentInfo($contentId);
// create a new location below the given parent
$newLocation = $locationService->createLocation($contentInfo,$locationCreateStruct);
// print out the new location
print_r($newLocation);
}
catch(\eZ\Publish\API\Repository\Exceptions\NotFoundException $e)
{
// react on content or location not found
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e)
{
// react on permission denied
$output->writeln($e->getMessage());
} |
Receipt 13 - Move or Copy Subtree
...
Code Block |
---|
language | php |
---|
title | copy/move location |
---|
|
// get the location service from the repsitory
$locationService = $repository->getLocationService();
try
{
// load the location from the given location id
$location = $locationService->loadLocation($locationId);
// load the parent location to move/copy to
$parentLocation = $locationService->loadLocation($parentLocationId);
if($operation == 'copy')
{
$newLocation = $locationService->copySubtree($location, $parentLocation);
}
else if($operation == 'move')
{
$newLocation = $locationService->moveSubtree($location, $parentLocation);
}
else
{
$output->writeln("operation must be copy or move");
return;
}
print_r($newLocation);
}
catch(\eZ\Publish\API\Repository\Exceptions\NotFoundException $e)
{
// react on parent location or location not found
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e)
{
// react on permission denied
$output->writeln($e->getMessage());
}
} |
Receipt 14 - Hide/Unhide Location
...
Code Block |
---|
language | php |
---|
title | hide/unhide location |
---|
|
// get the location service from the repository
$locationService = $repository->getLocationService();
// get the user service from the repsitory
$userService = $repository->getUserService();
// load admin user
$user = $userService->loadUser(14);
// set current user to admin
$repository->setCurrentUser($user);
try
{
// load the location info from the given location id
$location = $contentService->loadContentInfo($contentId);
// hide the location
$hiddenLocation = $locationService->hideLocation($location);
// print out the location
print_r($hiddenLocation);
// unhide the location
$unhiddenLocation = $locationService->unhideLocation($hiddenLocation);
// print out the location
print_r($unhiddenLocation);
}
catch(\eZ\Publish\API\Repository\Exceptions\NotFoundException $e)
{
// react on location not found
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e)
{
// react on permission denied
$output->writeln($e->getMessage());
} |
Receipt 15 - Deleting locations
...
Code Block |
---|
language | php |
---|
title | assign section to content |
---|
|
// get the content service from the repository
$contentService = $repository->getContentService();
// get the section service from the repsitory
$sectionService = $repository->getSectionService();
// get the user service from the repsitory
try
{
// load the content info from the given content id
$contentInfo = $contentService->loadContentInfo($contentId);
// load the section
$section = $sectionService->loadSection($sectionId);
// assign the section to the content
$sectionService->assignSection($contentInfo, $section);
// realod an print out
$contentInfo = $contentService->loadContentInfo($contentId);
// should show the new section id
$output->writeln($contentInfo->sectionId);
}
catch(\eZ\Publish\API\Repository\Exceptions\NotFoundException $e)
{
// react on content or section not found
$output->writeln($e->getMessage());
}
catch(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e)
{
// react on permission denied
$output->writeln($e->getMessage());
} |
...