You are here

public function WorkflowTypeBase::deleteState in Drupal 8

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

Deletes a state from the workflow.

Parameters

string $state_id: The state ID to delete.

Return value

$this The workflow type plugin.

Throws

\InvalidArgumentException Thrown if $state_id does not exist.

Overrides WorkflowTypeInterface::deleteState

1 method overrides WorkflowTypeBase::deleteState()
PredefinedStatesWorkflowTestType::deleteState in core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/PredefinedStatesWorkflowTestType.php
Deletes a state from the workflow.

File

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

Class

WorkflowTypeBase
A base class for Workflow type plugins.

Namespace

Drupal\workflows\Plugin

Code

public function deleteState($state_id) {
  if (!$this
    ->hasState($state_id)) {
    throw new \InvalidArgumentException("The state '{$state_id}' does not exist in workflow.");
  }
  if (count($this->configuration['states']) === 1) {
    throw new \InvalidArgumentException("The state '{$state_id}' can not be deleted from workflow as it is the only state.");
  }
  foreach ($this->configuration['transitions'] as $transition_id => $transition) {
    if ($transition['to'] === $state_id) {
      $this
        ->deleteTransition($transition_id);
      continue;
    }
    $from_key = array_search($state_id, $transition['from'], TRUE);
    if ($from_key !== FALSE) {

      // Remove state from the from array.
      unset($transition['from'][$from_key]);
      if (empty($transition['from'])) {

        // There are no more 'from' entries, remove the transition.
        $this
          ->deleteTransition($transition_id);
        continue;
      }

      // We changed the from state, update the transition.
      $this
        ->setTransitionFromStates($transition_id, $transition['from']);
    }
  }
  unset($this->configuration['states'][$state_id]);
  return $this;
}