Header image

Symfony Finland
Random things on PHP, Symfony and web development

How to search and find Content with the eZ Platform PHP API (part 3/3)

This is a code example from the eZ Publish / eZ Platform API Cookbook on how to search and find Content using the eZ Publish content API in Symfony Controllers or Commands:

 * File containing the FindContent3Command class.
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
 * @license For full copyright and license information view LICENSE file distributed with this source code.
 * @version //autogentag//

namespace EzSystems\CookbookBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;

 * This command performs a location and content type identifier filter search
class FindContent3Command extends ContainerAwareCommand
     * This method override configures on input argument for the content id
    protected function configure()
        $this->setName( 'ezpublish:cookbook:find_filter' )->setDefinition(
                new InputArgument( 'contentTypeIdentifier', InputArgument::REQUIRED, 'Content type identifier, one or several seperated by comma. example --contentTypeIdentifier=article,folder' ),
                new InputArgument( 'locationId', InputArgument::REQUIRED, 'Location id' ),

    protected function execute( InputInterface $input, OutputInterface $output )
        /** @var $repository \eZ\Publish\API\Repository\Repository */
        $repository = $this->getContainer()->get( 'ezpublish.api.repository' );
        $searchService = $repository->getSearchService();

        $text = $input->getArgument( 'text' );
        $contentTypeIdentifierList = explode( ',', $input->getArgument( 'contentTypeId' ) );
        $locationId = $input->getArgument( 'locationId' );

        // create the query with parent location id and a or condition of content type identifiers criteria
        $query = new \eZ\Publish\API\Repository\Values\Content\Query();
        $locationCriterion = new Criterion\ParentLocationId( $locationId );
        $congtentTypeOr = new Criterion\LogicalOr( array() );

        // Note: ContentTypeIdentifier is available in eZ Publish 5.1+, use ContentTypeId instead to also support 5.0
        foreach ( $contentTypeIdentifierList as $contentTypeIdentifier )
            $congtentTypeOr->criteria[] = new Criterion\ContentTypeIdentifier( $contentTypeIdentifier );

        $query->criterion = new Criterion\LogicalAnd(
            array( $locationCriterion, $congtentTypeOr )

        $result = $searchService->findContent( $query );
        $output->writeln( '<info>Found ' . $result->totalCount . ' items</info>' );
        foreach ( $result->searchHits as $searchHit )
            $output->writeln( "* " . $searchHit->valueObject->contentInfo->name );

Also see other examples of working with the eZ Platform to find content:

Learn more in the eZ Platform documentation or the eZ Platform Tips section on this site.

Written by Jani Tarvainen on Wednesday March 30, 2016
Permalink -

Leave a comment

comments powered by Disqus

« How to search and find Content with the eZ Platform PHP API (part 2/3) - How to find eZ Platform Bundles »