You are here

public function WorkflowTypeBase::setTransitionFromStates in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/workflows/src/Plugin/WorkflowTypeBase.php \Drupal\workflows\Plugin\WorkflowTypeBase::setTransitionFromStates()
  2. 10 core/modules/workflows/src/Plugin/WorkflowTypeBase.php \Drupal\workflows\Plugin\WorkflowTypeBase::setTransitionFromStates()

Sets a transition's from states.

Parameters

string $transition_id: The transition ID.

array $from_state_ids: The state IDs to transition from.

Return value

$this

Throws

\InvalidArgumentException Thrown if the transition does not exist or the states do not exist.

Overrides WorkflowTypeInterface::setTransitionFromStates

2 calls to WorkflowTypeBase::setTransitionFromStates()
WorkflowTypeBase::addTransition in core/modules/workflows/src/Plugin/WorkflowTypeBase.php
Adds a transition to the workflow.
WorkflowTypeBase::deleteState in core/modules/workflows/src/Plugin/WorkflowTypeBase.php
Deletes a state from the workflow.

File

core/modules/workflows/src/Plugin/WorkflowTypeBase.php, line 410

Class

WorkflowTypeBase
A base class for Workflow type plugins.

Namespace

Drupal\workflows\Plugin

Code

public function setTransitionFromStates($transition_id, array $from_state_ids) {
  if (!$this
    ->hasTransition($transition_id)) {
    throw new \InvalidArgumentException("The transition '{$transition_id}' does not exist in workflow.");
  }

  // Ensure that the states exist.
  foreach ($from_state_ids as $from_state_id) {
    if (!$this
      ->hasState($from_state_id)) {
      throw new \InvalidArgumentException("The state '{$from_state_id}' does not exist in workflow.");
    }
    if ($this
      ->hasTransitionFromStateToState($from_state_id, $this->configuration['transitions'][$transition_id]['to'])) {
      $existing_transition_id = $this
        ->getTransitionIdFromStateToState($from_state_id, $this->configuration['transitions'][$transition_id]['to']);
      if ($transition_id !== $existing_transition_id) {
        throw new \InvalidArgumentException("The '{$existing_transition_id}' transition already allows '{$from_state_id}' to '{$this->configuration['transitions'][$transition_id]['to']}' transitions in workflow.");
      }
    }
  }

  // Preserve the order of the state IDs in the from value and don't save any
  // keys.
  $from_state_ids = array_values($from_state_ids);
  sort($from_state_ids);
  $this->configuration['transitions'][$transition_id]['from'] = $from_state_ids;
  return $this;
}