Global navigation

   Documentation Center
   eZ Studio & eZ Platform
     User Manual
     Technical Manual
     Glossary
   eZ Publish 4.x / legacy

 
eZ Publish (5.x)

eZ Publish 5.x | For eZ Platform & eZ Studio topics see Technical manual and User manual, for eZ Publish 4.x and Legacy topics see eZ Publish legacy

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

We will start by going through the various ways to find and retrieve content from eZ Publish using the API. While this will be covered in further, dedicated documentation, it is necessary to explain a few basic concepts of the Public API.

The main entry point is always the Repository. Since the API relies on the Symfony Service Container (later referenced as SC), the repository is obtained using by calling the get() method of the service container, using the service identifier: ezpublish.api.repository. The container's availability depends on what class you're working in. By default, in a Controller, you can just call get() directly on the Controller object, $this. In a ContainerAwareCommand command line script, you first need to get the container using the getContainer() method.

Code Block
languagephp
titleGetting the repository from the service container
// In a ContainerAwareCommand subclass
$repository = $this->getContainer()->get( 'ezpublish.api.repository' );
 
// In a Controller
$repository = $this->get( 'ezpublish.api.repository' );
Info
titleGetting code completion to work on objects obtained via the service container

If you use a PHP IDE that supports complex code completion, you can use the following syntax to get completion on objects obtained via the service container:

Code Block
languagephp
/** @var $repository \eZ\Publish\API\Repository\Repository */
$repository = $this->getContainer()->get( 'ezpublish.api.repository' );

The repository allows you to request the various services that provide interaction with the system: content service, location service, user service...

Viewing Content

This recipe shows how to view fields of the content int the main language.

In the following recipes, you will learn about the general principles of the API as they are introduced in individual recipes.

Showing content of a Content's fields

In this recipe, we will see how to fetch a Content instance from the repository, and obtain its Field's content.

Let's first see the full code. You can see the Command line version at https://github.com/docb22/ez-publish-cookbook/blob/master/EzSystems/CookBookBundle/Command/ViewContentCommand.php.

Code Block
languagephp
titleviewing Viewing content
linenumberstrue
$repository = $this->getContainer()->get( 'ezpublish.api.repository' );
$contentService = $repository->getContentService();
$fieldTypeService = $repository->getFieldTypeService();


try
{
    // iterate over the field definitions of the content type and print out each field's identifier and value
    $content = $contentService->loadContent( $contentId );
    $contentType = $content->contentType;
    foreach( $contentType->fieldDefinitions as $fieldDefinition )
    {
        if( $fieldDefinition->fieldTypeIdentifier == 'ezpage' ) continue; // ignore ezpage
        $output->write( $fieldDefinition->identifier . ": " );
        $fieldType = $fieldTypeService->getFieldType( $fieldDefinition->fieldTypeIdentifier );
        $field = $content->getField( $fieldDefinition->identifier );
        $valueHash = $fieldType->toHash( $field->value ); // use toHash for getting a readable output
        $output->writeln( $valueHash );
    }
}
catch( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
{
    // if the id is not found
    $output->writeln( "No content with id $contentId" );
}
catch( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e )
{
    // not allowed to read this content
    $output->writeln( "Anonymous users are not allowed to read content with id $contentId" );
}

Let's analyze this code block by block.

Code Block
languagephp
linenumberstrue
$repository = $this->getContainer()->get( 'ezpublish.api.repository' );
$contentService = $repository->getContentService();
$fieldTypeService = $repository->getFieldTypeService();

This is the initialization part. As explained above, everything in the Public API goes through the repository via dedicated services. We get the repository from the service container, using the method get() of our container, obtained via $this->getContainer(). Using our $repository variable, we fetch the two services we will need using getContentService() and getFieldTypeService().

 

Info
titleUsed classes

...