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.
Comment: Update git repo name in search section

...

The first thing we do is use the Content Service to load a Content 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.

Code Block
languagephp
linenumberstrue
foreach( $content->contentType->fieldDefinitions as $fieldDefinition )
{
    // ignore ezpage
    if( $fieldDefinition->fieldTypeIdentifier == 'ezpage' )
        continue;
    $output->write( $fieldDefinition->identifier . ": " );
    $fieldType = $fieldTypeService->getFieldType( $fieldDefinition->fieldTypeIdentifier );
    $fieldValue = $content->getFieldValue( $fieldDefinition->identifier );
    $valueHash = $fieldType->toHash( $field->value );
    $output->writeln( $valueHash );
}

...

In this code, we introduce the LocationService. This service is used to interact with locations (eZ Publish 4 nodes). We use two methods from this service: loadLocation(), and loadLocationChildren().

Code Block
languagephp
titleLoading a location
try
{
    // load the starting location and browse
    $location = $this->locationService->loadLocation( $locationId );
    $this->browseLocation( $location, $output );
}
catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
{
    $output->writeln( "<error>No location found with id $locationId</error>" );
}
catch( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e )
{
    $output->writeln( "<error>Anonymous users are not allowed to read location with id $locationId</error>" );
}

As for the ContentService, loadLocation() returns a  Value Object, here a Location. Errors are handled with exceptions: NotFoundException if the Location ID couldn't be found, and UnauthorizedException if the current repository user isn't allowed to view this location.

Code Block
languagephp
titleIterating over a Location's children
private function browseLocation( Location $location, OutputInterface $output, $depth = 0 )
{
    $childLocations = $this->locationService->loadLocationChildren( $location );
    foreach ( $childLocations->locations as $childLocation )
    {
        $this->browseLocation( $childLocation, $output, $depth + 1 );
    }
}

LocationService::loadLocationChildren() returns an array of Location Value Objects that we can iterate over.

...

Info
titleFull code

Should you need more advanced children fetching methods, the SearchService is what you are looking for.

...

Content is a central piece in the Public API. You will often need to start from a Content, 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.

...

We introduce here several new services: URLAliasService, UserService and SectionService. The concept should be familiar to you now.

...

Tip

In a command line script, the repository runs as if executed by the anonymous user. In order to identify it as a different user, you need to use the UserService as follows:

Code Block
languagephp
$administratorUser = $userService()->loadUser( 14 );
$repository->setCurrentUser( $administratorUser );

This may be crucial when writing maintenance or synchronization scripts.

This is of course not required in template functions or controller code, as the HTTP layer will take care of identifying the user, and automatically set it in the repository.

...

Multiple criteria can be grouped together using "logical criteria", such as LogicalAnd or LogicalOr. Since in this case we only want to run a text search, we simply use a FullText criterion object.

Info

The full list of criteria available can be seen at: https://github.com/ezsystems/ezp-next/tree/master/eZ/Publish/API/Repository/Values/Content/Query/Criterion

...

The Query object is given as an argument to SearchService::findContent(). This method returns a SearchResult object. This object provides you with various information about the search operation (number of results, time taken, spelling suggestions, or facets, as well as of course, the results themselves.

...

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, or throws NotFound exception if none is found.

...

Code Block
languagephp
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content;
 
// [...]
 
$query = new Query();
$criterion1 = new Criterion\Subtree( '/location/path_string' );
$criterion2 = new Criterion\ContentTypeId( 1 );
$query->criterion = new Criterion\LogicalAnd(
    array( $criterion1, $criterion2 )
);
 
$result = $searchService->findContent( $query );

A Subtree criterion limits the search to the subtree with path_string /location/path_string. A ContentTypeId Criterion to limit the search to Content of ContentType 1. Those two criteria are grouped with a LogicalAnd operator. The query is executed as before, with SearchService::findContent().

Performing a fetch like search

...

Code Block
languagephp
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content;
 
// [...]
 
$query = new Query();
$query->criterion = new Criterion\LogicalAnd(
    array(
        new Criterion\LocationId( 2 ),
        new Criterion\LogicalOr(
            array(
                new Criterion\ContentTypeId( 1 ),
                new Criterion\ContentTypeId( 2 )
            )
        )
    )
);
 
$result = $searchService->findContent( $query );

LocationId criterion limits the search to the children of location 2. An array of of "ContentTypeId" Criteria to limit the search to Content of ContentType's with id 1 or 2 grouped in a LogicalOr operator. Those two criteria are grouped with a LogicalAnd operator. As always the query is executed as before, with SearchService::findContent().

Tip: Want to do a subtree filter ( in 4.x: fetch( 'content', 'tree' ) )? Change the location filter to use the Subtree criterion filter as shown in the advanced search example above.

...

Or if your on eZ Publish 5.1+ you can use the new ContentTypeIdentifier Criterion:

Code Block
languagephp
titleeZ Publish 5.1+ (2013.01+)
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content;
 
// [...]
 
$query = new Query();
$query->criterion = new Criterion\LogicalAnd(
    array(
        new Criterion\LocationId( 2 ),
        new Criterion\ContentTypeIdentifier( array( 'article', 'folder' ) )
    )
);
 
$result = $searchService->findContent( $query );

...

In many cases you might need the number of contents 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.

...