Forest header image

Symfony Finland
Random things on PHP, Symfony and web development

How to update 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:

<?php
/**
 * File containing the CreateContentCommand 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,
    Symfony\Component\Console\Input\InputInterface,
    Symfony\Component\Console\Output\OutputInterface,
    Symfony\Component\Console\Input\InputArgument,
    Symfony\Component\Console\Input\InputOption;

class UpdateContentCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName( 'ezpublish:cookbook:update_content' )->setDefinition(
            array(
                new InputArgument( 'contentId' , InputArgument::REQUIRED, 'the content to be updated' ),
                new InputArgument( 'newtitle' , InputArgument::REQUIRED, 'the new title of the content' ),
                new InputArgument( 'newbody' , InputArgument::REQUIRED, 'the new body of the content' )
            )
        );
    }

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

        $repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) );

        $contentId = $input->getArgument( 'contentId' );
        $newTitle = $input->getArgument( 'newtitle' );
        $newBody = $input->getArgument( 'newbody' );

        try
        {
            // create a content draft from the current published version
            $contentInfo = $contentService->loadContentInfo( $contentId );
            $contentDraft = $contentService->createContentDraft( $contentInfo );

            // instantiate a content update struct and set the new fields
            $contentUpdateStruct = $contentService->newContentUpdateStruct();
            $contentUpdateStruct->initialLanguageCode = 'eng-GB'; // set language for new version
            $contentUpdateStruct->setField( 'title', $newTitle );
            $contentUpdateStruct->setField( 'body', $newBody );

            // update and publish draft
            $contentDraft = $contentService->updateContent( $contentDraft->versionInfo, $contentUpdateStruct );
            $content = $contentService->publishVersion( $contentDraft->versionInfo );

            print_r( $content );
        }
        catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
        {
            $output->writeln( $e->getMessage() );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $e )
        {
            $output->writeln( $e->getMessage() );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\ContentValidationException $e )
        {
            $output->writeln( $e->getMessage() );
        }
    }
}

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 -

« How to copy and move Subtrees with the eZ Platform PHP API - How to load and view Content with the eZ Platform PHP API »