You are here

public function WorkflowTransitionEditForm::validateForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/workflows/src/Form/WorkflowTransitionEditForm.php \Drupal\workflows\Form\WorkflowTransitionEditForm::validateForm()
  2. 10 core/modules/workflows/src/Form/WorkflowTransitionEditForm.php \Drupal\workflows\Form\WorkflowTransitionEditForm::validateForm()

Form validation 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 FormBase::validateForm

File

core/modules/workflows/src/Form/WorkflowTransitionEditForm.php, line 131

Class

WorkflowTransitionEditForm
Class WorkflowTransitionEditForm.

Namespace

Drupal\workflows\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\workflows\WorkflowInterface $workflow */
  $workflow = $this
    ->getEntity();
  $workflow_type = $workflow
    ->getTypePlugin();
  $transition = $workflow_type
    ->getTransition($this->transitionId);
  $values = $form_state
    ->getValues();
  foreach (array_filter($values['from']) as $from_state_id) {
    if ($workflow_type
      ->hasTransitionFromStateToState($from_state_id, $values['to'])) {
      $existing_transition = $workflow_type
        ->getTransitionFromStateToState($from_state_id, $values['to']);
      if ($existing_transition
        ->id() !== $values['id']) {
        $form_state
          ->setErrorByName('from][' . $from_state_id, $this
          ->t('The transition from %from to %to already exists.', [
          '%from' => $workflow
            ->getTypePlugin()
            ->getState($from_state_id)
            ->label(),
          '%to' => $workflow
            ->getTypePlugin()
            ->getState($values['to'])
            ->label(),
        ]));
      }
    }
  }
  if ($workflow_type
    ->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
    $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
    $subform_state
      ->set('transition', $transition);
    $this->pluginFormFactory
      ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
      ->validateConfigurationForm($form['type_settings'], $subform_state);
  }
}