You are here

public function StateTransitionForm::submitForm in State Machine 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/StateTransitionForm.php, line 133

Class

StateTransitionForm

Namespace

Drupal\state_machine\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();

  /** @var \Drupal\state_machine\Plugin\Workflow\WorkflowTransition $transition */
  $transition = $triggering_element['#transition'];

  /** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
  $state_item = $this->entity
    ->get($this->fieldName)
    ->first();

  // Ensure the transition is still allowed before applying it.
  if ($state_item
    ->isTransitionAllowed($transition
    ->getId())) {
    $state_item
      ->applyTransition($triggering_element['#transition']);
    $this->entity
      ->save();
  }
}