Skip to the content
Documentation

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

No ratings yet

Versions Compared

Old Version 8

changes.mady.by.user Bertrand Dunogier

Saved on May 23, 2013

compared with

New Version 9

changes.mady.by.user Bertrand Dunogier

Saved on May 23, 2013

Key

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

...

Then, 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.

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 = \eZDB::instance();
            $transactionCounter = $db->transactionCounter();
            $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;
        }
    );
}

The code above takes a contentId (object ID), and a version number as the argument, and runs the content/publish operation on it. It then returns true if the operation ended successfully, or false if it was aborted by the workflow engine (or if it failed, but it can be improved).

Note
title Pay attention to the Repository user

Public API code is always executed as a user, by default anonymous. Since workflows are often user dependent (Approval is), it is important that this code is executed as an editor, or as a user who will trigger the workflow.

Code Block
title Logging a user in
language php
$repository->setCurrentUser( $repository->getUserService()->loadUser( $userId ) );

Of course, this isn't required when using Public API code in controllers, since those will use the currently-logged in user.

...

Comments

Add a comment

Comments are read by a moderator before they appear.