You are here

public function WorkflowConfigTransitionRoleForm::validateForm in Workflow 8

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

src/Form/WorkflowConfigTransitionRoleForm.php, line 129

Class

WorkflowConfigTransitionRoleForm
Defines a class to build a listing of Workflow Config Transitions entities.

Namespace

Drupal\workflow\Form

Code

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

  // 'Creation' state may not be the only state.
  if (count($form_state
    ->getValue($this->entitiesKey)) < 2) {
    $form_state
      ->setErrorByName('id', $this
      ->t('Please create at least one other state.'));
    return;
  }

  // Make sure 'author' is checked for (creation) -> [something].
  $creation_state_id = $this->workflow
    ->getCreationState()
    ->id();
  $author_has_permission = FALSE;
  foreach ($form_state
    ->getValue($this->entitiesKey) as $from_sid => $to_data) {
    foreach ($to_data as $to_sid => $transition_data) {
      if (empty($transition_data['roles'][WORKFLOW_ROLE_AUTHOR_RID])) {
        continue;
      }
      if ($from_sid == $creation_state_id && $from_sid != $to_sid) {
        $author_has_permission = TRUE;
        break;
      }
    }
  }
  if (!$author_has_permission) {
    $creation_state = $this->workflow
      ->getCreationState();
    $form_state
      ->setErrorByName('id', $this
      ->t('Please give the author permission to go from %creation to at least one state!', [
      '%creation' => $creation_state
        ->label(),
    ]));
  }
}