You are here

public function WorkflowTypeBase::addTransition in Drupal 8

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

Adds a transition to the workflow.

Parameters

string $id: The transition ID.

string $label: The transition's label.

array $from_state_ids: The state IDs to transition from.

string $to_state_id: The state ID to transition to.

Return value

$this

Throws

\InvalidArgumentException Thrown if either state does not exist.

Overrides WorkflowTypeInterface::addTransition

File

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

Class

WorkflowTypeBase
A base class for Workflow type plugins.

Namespace

Drupal\workflows\Plugin

Code

public function addTransition($transition_id, $label, array $from_state_ids, $to_state_id) {
  if ($this
    ->hasTransition($transition_id)) {
    throw new \InvalidArgumentException("The transition '{$transition_id}' already exists in workflow.");
  }
  if (preg_match(static::VALID_ID_REGEX, $transition_id)) {
    throw new \InvalidArgumentException("The transition ID '{$transition_id}' must contain only lowercase letters, numbers, and underscores.");
  }
  if (!$this
    ->hasState($to_state_id)) {
    throw new \InvalidArgumentException("The state '{$to_state_id}' does not exist in workflow.");
  }
  $this->configuration['transitions'][$transition_id] = [
    'label' => $label,
    'from' => [],
    'to' => $to_state_id,
    // Always add to the end.
    'weight' => $this
      ->getNextWeight($this->configuration['transitions']),
  ];
  try {
    $this
      ->setTransitionFromStates($transition_id, $from_state_ids);
  } catch (\InvalidArgumentException $e) {
    unset($this->configuration['transitions'][$transition_id]);
    throw $e;
  }
  ksort($this->configuration['transitions']);
  return $this;
}