Skip to content
Snippets Groups Projects

Workflow transition with context

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Brad Jones
    WorkflowTransitionWithContext.php 1.43 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Kinksters;
    
    use Drupal\state_machine\Plugin\Workflow\WorkflowState;
    use Drupal\state_machine\Plugin\Workflow\WorkflowTransition;
    
    /**
     * Workflow transition object with context.
     */
    final class WorkflowTransitionWithContext extends WorkflowTransition {
    
      /**
       * Context.
       */
      public \ArrayObject $context;
    
      /**
       * Constructs a new WorkflowTransition object.
       *
       * @param string $id
       *   The transition ID.
       * @param string $label
       *   The transition label.
       * @param \Drupal\state_machine\Plugin\Workflow\WorkflowState[] $from_states
       *   The "from" states.
       * @param \Drupal\state_machine\Plugin\Workflow\WorkflowState $to_state
       *   The "to" state.
       * @param array $context
       *   Context.
       */
      public function __construct(
        string $id,
        string $label,
        array $from_states,
        WorkflowState $to_state,
        array $context,
      ) {
        parent::__construct($id, $label, $from_states, $to_state);
        $this->context = new \ArrayObject($context);
      }
    
      /**
       * Create a workflow transition with context.
       *
       * @param WorkflowTransition $from
       *   Original transition object.
       * @param array $context
       *   Context.
       */
      public static function fromWorkflowTransition(WorkflowTransition $from, array $context): self {
        return new self(
          $from->getId(),
          $from->getLabel(),
          $from->getFromStates(),
          $from->getToState(),
          $context
        );
      }
    
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment