You are here

protected function StateItem::dispatchTransitionEvent in State Machine 8

Dispatches a transition event for the given phase.

Parameters

string $phase: The phase: pre_transition OR post_transition.

2 calls to StateItem::dispatchTransitionEvent()
StateItem::postSave in src/Plugin/Field/FieldType/StateItem.php
Defines custom post-save behavior for field values.
StateItem::preSave in src/Plugin/Field/FieldType/StateItem.php
Defines custom presave behavior for field values.

File

src/Plugin/Field/FieldType/StateItem.php, line 380

Class

StateItem
Plugin implementation of the 'state' field type.

Namespace

Drupal\state_machine\Plugin\Field\FieldType

Code

protected function dispatchTransitionEvent($phase) {

  /** @var \Drupal\state_machine\Plugin\Workflow\WorkflowInterface $workflow */
  $workflow = $this
    ->getWorkflow();
  $transition = $this->transitionToApply ?? $workflow
    ->findTransition($this->originalValue, $this->value);
  if ($transition) {
    $field_name = $this
      ->getFieldDefinition()
      ->getName();
    $group_id = $workflow
      ->getGroup();
    $transition_id = $transition
      ->getId();
    $event_dispatcher = \Drupal::getContainer()
      ->get('event_dispatcher');
    $event = new WorkflowTransitionEvent($transition, $workflow, $this
      ->getEntity(), $field_name);
    $events = [
      // For example: 'commerce_order.place.pre_transition'.
      $group_id . '.' . $transition_id . '.' . $phase,
      // For example: 'commerce_order.pre_transition'.
      $group_id . '.' . $phase,
      // For example: 'state_machine.pre_transition'.
      'state_machine.' . $phase,
    ];
    foreach ($events as $event_id) {
      $event_dispatcher
        ->dispatch($event_id, $event);
    }
  }
}