You are here

class WorkflowTransitionEventSubscriber in State Machine 8

Hierarchy

Expanded class hierarchy of WorkflowTransitionEventSubscriber

1 string reference to 'WorkflowTransitionEventSubscriber'
state_machine_test.services.yml in tests/modules/state_machine_test/state_machine_test.services.yml
tests/modules/state_machine_test/state_machine_test.services.yml
1 service uses WorkflowTransitionEventSubscriber
state_machine_test.event_subscriber in tests/modules/state_machine_test/state_machine_test.services.yml
Drupal\state_machine_test\EventSubscriber\WorkflowTransitionEventSubscriber

File

tests/modules/state_machine_test/src/EventSubscriber/WorkflowTransitionEventSubscriber.php, line 10

Namespace

Drupal\state_machine_test\EventSubscriber
View source
class WorkflowTransitionEventSubscriber implements EventSubscriberInterface {

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a new WorkflowTransitionEventSubscriber object.
   *
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(MessengerInterface $messenger) {
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'entity_test_with_bundle.create.pre_transition' => 'onPreTransition',
      'entity_test_with_bundle.create.post_transition' => 'onPostTransition',
      'entity_test_with_bundle.pre_transition' => 'onGroupPreTransition',
      'entity_test_with_bundle.post_transition' => 'onGroupPostTransition',
      'state_machine.pre_transition' => 'onGenericPreTransition',
      'state_machine.post_transition' => 'onGenericPostTransition',
    ];
    return $events;
  }

  /**
   * Reacts to the 'entity_test_with_bundle.create.pre_transition' event.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onPreTransition(WorkflowTransitionEvent $event) {
    $this
      ->setMessage($event, 'pre-transition');
  }

  /**
   * Reacts to the 'entity_test_with_bundle.create.post_transition' event.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onPostTransition(WorkflowTransitionEvent $event) {
    $this
      ->setMessage($event, 'post-transition');
  }

  /**
   * Reacts to the 'entity_test_with_bundle.pre_transition' event.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onGroupPreTransition(WorkflowTransitionEvent $event) {
    $this
      ->setMessage($event, 'group pre-transition');
  }

  /**
   * Reacts to the 'entity_test_with_bundle.post_transition' event.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onGroupPostTransition(WorkflowTransitionEvent $event) {
    $this
      ->setMessage($event, 'group post-transition');
  }

  /**
   * Reacts to the 'state_machine.pre_transition' event.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onGenericPreTransition(WorkflowTransitionEvent $event) {
    $this
      ->setMessage($event, 'generic pre-transition');
  }

  /**
   * Reacts to the 'state_machine.post_transition' event.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onGenericPostTransition(WorkflowTransitionEvent $event) {
    $this
      ->setMessage($event, 'generic post-transition');
  }

  /**
   * Sets a message with event information for test purposes.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   * @param string $phase
   *   The phase during which the event occurred.
   */
  protected function setMessage(WorkflowTransitionEvent $event, $phase) {
    $this->messenger
      ->addMessage(new TranslatableMarkup('@entity_label (@field_name) - @state_label at @phase (workflow: @workflow, transition: @transition).', [
      '@entity_label' => $event
        ->getEntity()
        ->label(),
      '@field_name' => $event
        ->getFieldName(),
      '@state_label' => $event
        ->getTransition()
        ->getToState()
        ->getLabel(),
      '@workflow' => $event
        ->getWorkflow()
        ->getId(),
      '@transition' => $event
        ->getTransition()
        ->getId(),
      '@phase' => $phase,
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WorkflowTransitionEventSubscriber::$messenger protected property The messenger.
WorkflowTransitionEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
WorkflowTransitionEventSubscriber::onGenericPostTransition public function Reacts to the 'state_machine.post_transition' event.
WorkflowTransitionEventSubscriber::onGenericPreTransition public function Reacts to the 'state_machine.pre_transition' event.
WorkflowTransitionEventSubscriber::onGroupPostTransition public function Reacts to the 'entity_test_with_bundle.post_transition' event.
WorkflowTransitionEventSubscriber::onGroupPreTransition public function Reacts to the 'entity_test_with_bundle.pre_transition' event.
WorkflowTransitionEventSubscriber::onPostTransition public function Reacts to the 'entity_test_with_bundle.create.post_transition' event.
WorkflowTransitionEventSubscriber::onPreTransition public function Reacts to the 'entity_test_with_bundle.create.pre_transition' event.
WorkflowTransitionEventSubscriber::setMessage protected function Sets a message with event information for test purposes.
WorkflowTransitionEventSubscriber::__construct public function Constructs a new WorkflowTransitionEventSubscriber object.