getTransition example

$this->assertTrue($net->buildTransitionBlockerList($subject, 't3')->isEmpty());
    }

    public function testBuildTransitionBlockerListReturnsExpectedReasonOnBranchMerge()
    {
        $definition = $this->createComplexStateMachineDefinition();

        $dispatcher = new EventDispatcher();
        $net = new StateMachine($definition, null, $dispatcher);

        $dispatcher->addListener('workflow.guard', function DGuardEvent $event) {
            $event->addTransitionBlocker(new TransitionBlocker(sprintf('Transition blocker of place %s', $event->getTransition()->getFroms()[0]), 'blocker'));
        });

        $subject = new Subject();

        // There may be multiple transitions with the same name. Make sure that transitions         // that are enabled by the marking are evaluated.         // see https://github.com/symfony/symfony/issues/28432
        // Test if when you are in place "a" and trying to apply "t1" then it returns         // blocker list contains guard blocker instead blockedByMarking         $subject->setMarking('a');
        

  public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_transition = NULL) {
    try {
      $this->transition = $workflow->getTypePlugin()->getTransition($workflow_transition);
    }
    catch (\InvalidArgumentException $e) {
      throw new NotFoundHttpException();
    }
    $this->workflow = $workflow;
    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  
$this->clickLink('Add a new state');
    $this->submitForm(['label' => 789, 'id' => 789], 'Save');
    $this->assertSession()->pageTextContains('Created 789 state.');

    $this->clickLink('Add a new transition');
    $this->submitForm(['id' => 101112, 'label' => 101112, 'from[456]' => 456, 'to' => 789], 'Save');
    $this->assertSession()->pageTextContains('Created 101112 transition.');

    $workflow = $this->container->get('entity_type.manager')->getStorage('workflow')->loadUnchanged(123);
    $this->assertEquals(123, $workflow->id());
    $this->assertEquals(456, $workflow->getTypePlugin()->getState(456)->id());
    $this->assertEquals(101112, $workflow->getTypePlugin()->getTransition(101112)->id());
    $this->assertEquals(789, $workflow->getTypePlugin()->getTransition(101112)->to()->id());
  }

  /** * Tests the sorting of states and transitions by weight and label. */
  public function testSorting() {
    $workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test', 'label' => 'Test']);
    $workflow
      ->getTypePlugin()
      ->setConfiguration([
        
public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /** * @return void */
    public function onLeave(Event $event)
    {
        foreach ($event->getTransition()->getFroms() as $place) {
            $this->logger->info(sprintf('Leaving "%s" for subject of class "%s" in workflow "%s".', $place$event->getSubject()::class$event->getWorkflowName()));
        }
    }

    /** * @return void */
    public function onTransition(Event $event)
    {
        $this->logger->info(sprintf('Transition "%s" for subject of class "%s" in workflow "%s".', $event->getTransition()->getName()$event->getSubject()::class$event->getWorkflowName()));
    }

    


    public function getContext(): array
    {
        trigger_deprecation('symfony/workflow', '6.4', 'The %s::getContext() method is deprecated and will be removed in 7.0. You should no longer call this method as it always returns an empty array when invoked within a guard listener.', __CLASS__);

        return parent::getContext();
    }

    public function getTransition(): Transition
    {
        return parent::getTransition();
    }

    public function isBlocked(): bool
    {
        return !$this->transitionBlockerList->isEmpty();
    }

    public function setBlocked(bool $blocked, string $message = null): void
    {
        if (!$blocked) {
            $this->transitionBlockerList->clear();

            

    public function onTransition(GuardEvent $event, string $eventName)
    {
        if (!isset($this->configuration[$eventName])) {
            return;
        }

        $eventConfiguration = (array) $this->configuration[$eventName];
        foreach ($eventConfiguration as $guard) {
            if ($guard instanceof GuardExpression) {
                if ($guard->getTransition() !== $event->getTransition()) {
                    continue;
                }
                $this->validateGuardExpression($event$guard->getExpression());
            } else {
                $this->validateGuardExpression($event$guard);
            }
        }
    }

    private function validateGuardExpression(GuardEvent $event, string $expression): void
    {
        
'state_machine.order_delivery.state_changed' => 'onOrderDeliveryStateChange',
            'state_machine.order_transaction.state_changed' => 'onOrderTransactionStateChange',
            BusinessEventCollectorEvent::NAME => 'onAddStateEvents',
        ];
    }

    /** * @throws OrderException */
    public function onOrderDeliveryStateChange(StateMachineStateChangeEvent $event): void
    {
        $orderDeliveryId = $event->getTransition()->getEntityId();

        $criteria = new Criteria([$orderDeliveryId]);
        $criteria->addAssociation('order.orderCustomer');
        $criteria->addAssociation('order.transactions.stateMachineState');

        /** @var OrderDeliveryEntity|null $orderDelivery */
        $orderDelivery = $this->deliveryRepository
            ->search($criteria$event->getContext())
            ->first();

        if ($orderDelivery === null) {
            
return $this->getTransitions($transition_ids);
  }

  /** * {@inheritdoc} */
  public function getTransitionFromStateToState($from_state_id$to_state_id) {
    $transition_id = $this->getTransitionIdFromStateToState($from_state_id$to_state_id);
    if (empty($transition_id)) {
      throw new \InvalidArgumentException("The transition from '$from_state_id' to '$to_state_id' does not exist in workflow.");
    }
    return $this->getTransition($transition_id);
  }

  /** * {@inheritdoc} */
  public function hasTransitionFromStateToState($from_state_id$to_state_id) {
    return $this->getTransitionIdFromStateToState($from_state_id$to_state_id) !== NULL;
  }

  /** * Gets the transition ID from state to state. * * @param string $from_state_id * The state ID to transition from. * @param string $to_state_id * The state ID to transition to. * * @return string|null * The transition ID, or NULL if no transition exists. */


  /** * {@inheritdoc} */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form$form_state);

    /** @var \Drupal\workflows\WorkflowInterface $workflow */
    $workflow = $this->getEntity();
    $workflow_type = $workflow->getTypePlugin();
    $transition = $workflow->getTypePlugin()->getTransition($this->transitionId);

    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Transition label'),
      '#maxlength' => 255,
      '#default_value' => $transition->label(),
      '#required' => TRUE,
    ];

    $form['id'] = [
      '#type' => 'value',
      
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');

    // By default states are ordered in the order added.     $workflow
      ->getTypePlugin()
      ->addState('draft', 'Draft')
      ->addState('published', 'Published');

    $this->assertFalse($workflow->getTypePlugin()->getState('draft')->canTransitionTo('published'));
    $workflow->getTypePlugin()->addTransition('publish', 'Publish', ['draft'], 'published');
    $this->assertTrue($workflow->getTypePlugin()->getState('draft')->canTransitionTo('published'));
    $this->assertEquals(0, $workflow->getTypePlugin()->getTransition('publish')->weight());
    $this->assertTrue($workflow->getTypePlugin()->hasTransition('publish'));
    $this->assertFalse($workflow->getTypePlugin()->hasTransition('draft'));

    $workflow->getTypePlugin()->addTransition('save_publish', 'Save', ['published'], 'published');
    $this->assertEquals(1, $workflow->getTypePlugin()->getTransition('save_publish')->weight());
  }

  /** * @covers ::addTransition */
  public function testAddTransitionDuplicateException() {
    
public function testWithGuardExpressionWithNotSupportedTransition()
    {
        $event = $this->createEvent();
        $this->configureValidator(false);
        $this->listener->onTransition($event, 'test_expression');

        $this->assertFalse($event->isBlocked());
    }

    public function testWithGuardExpressionWithSupportedTransition()
    {
        $event = $this->createEvent($this->configuration['test_expression'][1]->getTransition());
        $this->configureValidator(true, true);
        $this->listener->onTransition($event, 'test_expression');

        $this->assertFalse($event->isBlocked());
    }

    public function testGuardExpressionBlocks()
    {
        $event = $this->createEvent($this->configuration['test_expression'][1]->getTransition());
        $this->configureValidator(true, false);
        $this->listener->onTransition($event, 'test_expression');

        
->validateConfigurationForm($form['type_settings']$subform_state);
    }
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    /** @var \Drupal\workflows\WorkflowInterface $workflow */
    $workflow = $this->entity;
    $workflow_type = $workflow->getTypePlugin();
    $transition = $workflow_type->getTransition($form_state->getValue('id'));

    if ($workflow_type->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
      $subform_state = SubformState::createForSubform($form['type_settings']$form$form_state);
      $subform_state->set('transition', $transition);
      $this->pluginFormFactory
        ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
        ->submitConfigurationForm($form['type_settings']$subform_state);
    }

    $workflow->save();
    $this->messenger()->addStatus($this->t('Created %label transition.', [
      
Home | Imprint | This part of the site doesn't use cookies.