Header image

Symfony Finland
Random things on PHP, Symfony and web development

How to load and view Content with the eZ Platform PHP API

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 ViewContentCommand 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;

 * This command loads a Content using its numerical identifier, iterates over the Content's Fields, and shows the value
 * of each of them.
class ViewContentCommand extends ContainerAwareCommand
    protected function configure()
        $this->setName( 'ezpublish:cookbook:view_content' )->setDefinition(
                new InputArgument( 'contentId', InputArgument::REQUIRED, 'An existing content id' )

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

        $contentId = $input->getArgument( 'contentId' );

            $content = $contentService->loadContent( $contentId );
            $contentType = $contentTypeService->loadContentType( $content->contentInfo->contentTypeId );

            // Iterate over the field definitions of the content type, and print identifier: value
            foreach ( $contentType->fieldDefinitions as $fieldDefinition )
                $output->writeln( "<info>" . $fieldDefinition->identifier . "</info>" );
                $fieldType = $fieldTypeService->getFieldType( $fieldDefinition->fieldTypeIdentifier );
                $field = $content->getField( $fieldDefinition->identifier );
                $valueHash = $fieldType->toHash( $field->value );
                $output->writeln( $valueHash );
        catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
            $output->writeln( "<error>No content with id $contentId found</error>" );
        catch ( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e )
            $output->writeln( "<error>Permission denied on content with id $contentId</error>" );

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 update Content with the eZ Platform PHP API - How to load and view Content Metadata with the eZ Platform PHP API »