Skip to the content
Documentation

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

No ratings yet

Versions Compared

Old Version 16

changes.mady.by.user Ricardo Correia

Saved on Jun 26, 2013

compared with

New Version 17

changes.mady.by.user Bertrand Dunogier

Saved on Jun 27, 2013

Key

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

...

Create a draft using the public API

This is done the the usual way, as extensively covered in the Public API Cookbook. The main different is that we will NOT call ContentService::publishVersion().

...

Publish a Public API draft using the Legacy Kernel

First, a method must be used to obtain a legacy kernel. This can be used, given that getContainer() returns the Symfony Dependency Injection Container:

Code Block
language php
/**
 * Returns the legacy kernel object.
 *
 * @return \eZ\Publish\Core\MVC\Legacy\Kernel
 */
protected function getLegacyKernel()
{
    $closure = $this->getContainer()->get( 'exponential_legacy.kernel' );
    return $closure();
}

Then, you  

You need to use the object to run a Legacy Kernel Callback (e.g. code fragment). This legacy code will execute the publish operation with the given content id and version number. It will then check the operation result, and return true or false, depending if the operation went all the way through, or if it was interrupted (approval workflow event, for instance). This handling could and should of course be enhanced, with error handling to begin with, but it doesn't change the core principle.

This example will work in any controller that extends eZ\Publish\EzPublishCoreBundle\Controller:

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, $runAsUserId = null )
{
    $legacyKernel = $this->getLegacyKernel();
    return $legacyKernel->runCallback(
        function () use ( $contentId, $versionId, $runAsUserId )
        {
            $script = \eZScript::instance( array( 'use-modules' => true ) );
            $script->initialize();
            $db = \eZDB::instance();
            $transactionCounter = $db->transactionCounter();
            if ( $runAsUserId )
            {
                \eZUser::setCurrentlyLoggedInUser( \eZUser::fetch( $runAsUserId ), $runAsUserId );
            }
            $operationResult = \eZOperationHandler::execute(
                'content', 'publish', array( 'object_id' => $contentId, 'version' => $versionId )
            );
            if ( ( array_key_exists( 'status', $operationResult ) && $operationResult['status'] != \eZModuleOperationInfo::STATUS_CONTINUE ) )
            {
                // Required by https://jira.ez.no/browse/EZP-20558
                for ( $i = 0, $counter = ( $db->transactionCounter() - $transactionCounter ); $i < $counter; ++$i )
                {
                    $db->commit();
                }
                return false;
            }
            return true;
        }
    );
}

...

Comments

Add a comment

Comments are read by a moderator before they appear.