You are here

public function StateItem::applyTransition in State Machine 8

Applies the given transition, changing the current state.

Parameters

\Drupal\state_machine\Plugin\Workflow\WorkflowTransition $transition: The transition to apply.

Throws

\InvalidArgumentException Thrown when the transition is not allowed.

Overrides StateItemInterface::applyTransition

1 call to StateItem::applyTransition()
StateItem::applyTransitionById in src/Plugin/Field/FieldType/StateItem.php
Applies a transition with the given ID, changing the current state.

File

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

Class

StateItem
Plugin implementation of the 'state' field type.

Namespace

Drupal\state_machine\Plugin\Field\FieldType

Code

public function applyTransition(WorkflowTransition $transition) {
  if (!$this
    ->isTransitionAllowed($transition
    ->getId())) {
    throw new \InvalidArgumentException(sprintf('The transition "%s" is currently not allowed. (Current state: "%s".)', $transition
      ->getId(), $this
      ->getId()));
  }

  // Store the transition to apply, to ensure we're applying the requested
  // transition instead of guessing based on the original state.
  $this->transitionToApply = $transition;
  $this
    ->setValue([
    'value' => $transition
      ->getToState()
      ->getId(),
  ]);
}