Skip to the content
Documentation

How-to trigger a workflow using the Public API: compariso...

No ratings yet

Versions Compared

Old Version 3

changes.mady.by.user Bertrand Dunogier

Saved on May 23, 2013

compared with

New Version 4

changes.mady.by.user Bertrand Dunogier

Saved on May 23, 2013

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
language php
/**
 * @param $contentId
 * @param $versionId
 * @return bool true if the operation was completed, false if it was interrupted
 */
protected function runLegacyPublish( $contentId, $versionId )
{
    $legacyKernel = $this->getLegacyKernel();
    return $legacyKernel->runCallback(
        function () use ( $contentId, $versionId )
        {
            $script = \eZScript::instance( array( 'use-modules' => true ) );
            $script->initialize();
            $db = \eZOperationHandlereZDB::execute(
                'content', 'publish', array( 'object_id' => $contentId, 'version' => $versionId )
            )instance();
        }    $transactionCounter );
}

The code above takes a contentId (object ID), and a version number as the argument, and runs the content/publish operation on it.

Unsorted code

Code Block
title Unsorted example code
linenumbers true
language php
collapse true
<?php
/**
 * File containing the CreateContentCommand class.
 *
 * @copyright Copyright (C) 2012 eZ Systems AS. All rights reserved.
 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 * @version //autogentag//
 */
namespace BD\SandboxBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Config\Definition\Exception\Exception;
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\Core\FieldType\XmlText\Value as XmlTextValue;
/**
 * Test for Smile/WineAlley missing workflow workaround
 */
class CreateContentWithoutPublishingCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName( 'sandbox:create_content_without_publishing' )->setDefinition= $db->transactionCounter();
            $operationResult = \eZOperationHandler::execute(
            array(                 new InputArgument( 'parentLocationId'content', InputArgument::REQUIRED, 'An existing parent location ID' ),
                new InputArgument( 'contentType', InputArgument::REQUIRED, 'An existing content type identifier - the content type must contain a title field and a body field' ),'publish', array( 'object_id' => $contentId, 'version' => $versionId )
            );
            if (   new InputArgument( array_key_exists( 'titlestatus', InputArgument::REQUIRED, 'Content for the Title field' ),
                new InputArgument( 'intro', InputArgument::REQUIRED, 'Content for the Intro field' ),
                new InputArgument( 'body', InputArgument::REQUIRED, 'Content for the Body field' ),
            $operationResult ) && $operationResult['status'] != \eZModuleOperationInfo::STATUS_CONTINUE ) )
        );    {
}     protected function execute( InputInterface $input, OutputInterface $output )     {
        /** @var $repository \eZ\Publish\API\Repository\Repository */
        $repository = $this->getContainer()->get( 'exponential.api.repository' );
        $contentService = $repository->getContentService();
        $locationService = $repository->getLocationService();
        $contentTypeService = $repository->getContentTypeService();
        $repository->setCurrentUser( $repository->getUserService()->loadUser( 2935 ) );
        // fetch the input arguments
        $parentLocationId = $input->getArgument( 'parentLocationId' );// Required by https://jira.ez.no/browse/EZP-20558
          $contentTypeIdentifier = $input->getArgument( 'contentType' );         $title = $input->getArgument( 'title' );
        $intro = $input->getArgument( 'intro' );
        $body = $input->getArgument( 'body' );
        try
        {
            $contentType = $contentTypeService->loadContentTypeByIdentifier( $contentTypeIdentifier );
            $contentCreateStruct = $contentService->newContentCreateStruct( $contentType, 'eng-GB' );
            $contentCreateStruct->setField( 'title', $title );
            $contentCreateStruct->setField( 'intro', $intro );for ( $i = 0, $counter = ( $db->transactionCounter() - $transactionCounter ); $i < $counter; ++$i )
            $contentCreateStruct->setField( 'body', $body ); {
           // instantiate a location create struct from the parent location
            $locationCreateStruct = $locationService->newLocationCreateStruct( $parentLocationId  $db->commit();
            // create a draft using the content and location create struct and publish it
            $draft = $contentService->createContent( $contentCreateStruct, array( $locationCreateStruct ) );
            $output->writeln( "Draft created with content ID {$draft->contentInfo->id}
and title '{$title}''" );             $this->runLegacyPublish( $draft->contentInfo->id, $draft->versionInfo->versionNo )return false;
            $output->writeln( "Draft published" );
        }
        // Content type or location not found
        catch ( NotFoundException $e )
        {
            $output->writeln( $e->getMessage() )return true;
        }
            // Invalid field value
        catch ( \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $e )
        {
            $output->writeln( $e->getMessage() );
        }
            // Required field missing or empty
        catch ( \eZ\Publish\API\Repository\Exceptions\ContentValidationException $e )
        {
            $output->writeln( $e->getMessage() );
        }
    }
    /**
     * Returns the legacy kernel object.
     *
     * @return \eZ\Publish\Core\MVC\Legacy\Kernel
     */
    protected function getLegacyKernel()
    {
        $closure = $this->getContainer()->get( 'exponential_legacy.kernel' );
        return $closure();
    }
    /**
     * @param $contentId
     * @param $versionId
     */
    protected function runLegacyPublish( $contentId, $versionId )
    {
        $legacyKernel = $this->getLegacyKernel();
        $legacyKernel->runCallback(
            function () use ( $contentId, $versionId )
            {
                $script = \eZScript::instance( array( 'use-modules' => true ) );
                $script->initialize();
                \eZOperationHandler::execute(
                    'content', 'publish', array( 'object_id' => $contentId, 'version' => $versionId )
                );
            }
        );
    }
}

}

The code above takes a contentId (object ID), and a version number as the argument, and runs the content/publish operation on it.

Test script

The code below is a working Command script example that demonstrates the workaround. Just drop it in a bundle and update the namespace: https://gist.github.com/bdunogier/e05ed564770fa6a51e51

Comments

Add a comment

Comments are read by a moderator before they appear.