Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example how to make transition of workflow using processes in Oro
definitions:
order_create:
label: 'Trigger "final_step_transition" transition for new FooBar entity if FooBar->isSubscribed() === true'
entity: ACME\Bundle\DemoBundle\Entity\FooBar
actions_configuration:
- @transit_workflow:
conditions:
@true: $subscribed
parameters: [$.data, 'final_step_transition']
triggers:
order_create:
-
event: create
priority: 5
queued: false
services:
acme_demo.action.transit_workflow:
class: ACME\Bundle\DemoBundle\Model\Action\TransitWorkflow
arguments:
- @oro_workflow.context_accessor
- @oro_workflow.manager
tags:
- { name: oro_workflow.action, alias: transit_workflow }
<?php
namespace ACME\Bundle\DemoBundle\Model\Action;
use Oro\Bundle\WorkflowBundle\Exception\ActionException;
use Oro\Bundle\WorkflowBundle\Exception\InvalidParameterException;
use Oro\Bundle\WorkflowBundle\Model\Action\AbstractAction;
use Oro\Bundle\WorkflowBundle\Model\ContextAccessor;
use Oro\Bundle\WorkflowBundle\Model\WorkflowManager;
/**
* Performs workflow transition
*/
class TransitWorkflow extends AbstractAction
{
/**
* @var WorkflowManager
*/
protected $workflowManager;
/**
* @var string
*/
protected $entity;
/**
* @var string
*/
protected $transition;
/**
* @param ContextAccessor $contextAccessor
* @param WorkflowManager $workflowManager
*/
public function __construct(ContextAccessor $contextAccessor, WorkflowManager $workflowManager)
{
parent::__construct($contextAccessor);
$this->workflowManager = $workflowManager;
}
/**
* {@inheritdoc}
*/
protected function executeAction($context)
{
$entity = $this->contextAccessor->getValue($context, $this->entity);
$transition = $this->contextAccessor->getValue($context, $this->transition);
$workflowItem = $this->workflowManager->getWorkflowItemByEntity($entity);
if (!$workflowItem) {
throw new ActionException('Cannot transit workflow, entity doesn\'t have workflow.');
}
$this->workflowManager->transit($workflowItem, $transition);
}
/**
* {@inheritdoc}
*/
public function initialize(array $options)
{
if (isset($options['entity'])) {
$this->entity = $options['entity'];
} elseif (isset($options[0])) {
$this->entity = $options[0];
} else {
throw new InvalidParameterException('Option "entity" is required.');
}
if (isset($options['transition'])) {
$this->transition = $options['transition'];
} elseif (isset($options[1])) {
$this->transition = $options[1];
} else {
throw new InvalidParameterException('Option "transition" is required.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment