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. In the following recipes, you will learn about the general principles of the API as they are introduced in individual recipes.

Table of Contents

Displaying values from a Content item

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

...

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

try
{
    $content = $contentService->loadContent( 66 );
    $contentType = $contentTypeService->loadContentType( $content->contentInfo->contentTypeId );
    // iterate over the field definitions of the content type and print out each field's identifier and value
    foreach( $contentType->fieldDefinitions as $fieldDefinition )
    {
        $output->write( $fieldDefinition->identifier . ": " );
        $fieldType = $fieldTypeService->getFieldType( $fieldDefinition->fieldTypeIdentifier );
        $field = $content->getFieldValue>getField( $fieldDefinition->identifier );

		// We use the Field's toHash() method to get readable content out of the Field
        $valueHash = $fieldType->toHash( $field->value );
        $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" );
}

...

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

...

The first thing we do is use the Content Service to load a Content item using its ID, 66: $contentService->loadContent( 66 ). As you can see on the API doc page, this method expects a Content ID, and returns a Content Value Object.

...

Info
titleLoading Content in different languages

Since we didn't specify any language code, our Field objects object is returned in the default language, depending on your language settings in ezpublish.yml. If you want to use a non-default given Content item's main language. If you'd prefer it to fall back to the SiteAccess language(s), then take advantage of TranslationHelpers. Or if you want to use an altogether different language, you can specify a language code in the getField() call:

Code Block
languagephp
$content->getFieldValue( $fieldDefinition->identifier, 'fre-FR' )

...

Content is a central piece in the Public API. You will often need to start from a Content item, and dig in from its metadata. Basic content metadata is made available through ContentInfo objects. This Value Object mostly provides primitive fields: contentTypeId, publishedDate or mainLocationId. But it is also used to request further Content related Value Objects from various services.

...

Note
titleLimitations with multi-location

(version <= 5.2)

When Content has multiple Locations, note that sorting on Location properties cannot  work correctly. It's a limitation by design : SortClause\LocationPriority will not be limited to the locations under Criterion\ParentLocationId.

Info
titleLocation Search in 5.3

This limitation does not exists exist in Location Search. Migrating to version 5.3 adds the Location Search feature, and remove removes this limitation.
With Location Search, usage of Location Criterions and Sort Clauses with Content Search is deprecated. If search by Location properties is needed Location Search should be used.

...

In this section we will cover how the SearchService can be used to search for ContententContent, by using a Query and a combinations of Criteria you will get a SearchResult object back containing list of Content and count of total hits. In the future this object will also include facets, spell checking and "more like this" when running on a backend that supports it (for instance Solr).

...

Tip: If you you are searching using a unique identifier, for instance using the content id or content remote id criterion, then you can use SearchService::findSingle(), this takes a Criterion and returns a single Content item, or throws NotFound exception if none is found.

...

In many cases you might need the number of contents Content items matching a search, but with no need to do anything else with the results.
Thanks to the fact that the "searchHits" property of the SearchResult object always refers to the total amount, it is enough to run a standard search and set $limit to 0. This way no results will be retrieved, and the search will not be slowed down, even when the number of matching results is huge.

...