You are here

public function CancelButtonSettingsForm::validateForm in Entity Form Cancel Button 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/CancelButtonSettingsForm.php, line 228

Class

CancelButtonSettingsForm
Configure generic settings for the Cancel Button.

Namespace

Drupal\cancel_button\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $result = $this
    ->getEntityTypesToDisplay();
  $entity_types = $result["entity_types"];
  $bundles = $result["bundles"];
  $entity_types += [
    'default' => NULL,
  ];

  // Based on whether the entity type has bundles or not, the fieldsets
  // contain settings for either the entity type or the bundles.
  // Validate appropriately.
  foreach ($entity_types as $id => $type) {
    if (!(array_key_exists($id, $bundles) && count($bundles[$id]) > 0)) {
      $field = $id . '_cancel_destination';
      $value = $form_state
        ->getValue($field);
      if (empty($value) && $form_state
        ->getValue($id . '_cancel_enabled')) {
        $form_state
          ->setErrorByName($field, $this
          ->t('You must enter a path in this field'));
        $form['entity_type_cancel_destination'][$id]['#open'] = TRUE;
      }
      elseif (!$this->pathValidator
        ->isValid($value)) {
        $form_state
          ->setErrorByName($field, $this
          ->t("The path '%path' is either invalid or you do not have access to it.", [
          '%path' => $value,
        ]));
        $form['entity_type_cancel_destination'][$id]['#open'] = TRUE;
      }
    }
    if (array_key_exists($id, $bundles) && count($bundles[$id]) > 0) {
      foreach ($bundles[$id] as $bundle) {
        $field = $id . '_' . $bundle
          ->id() . '_cancel_destination';
        $value = $form_state
          ->getValue($field);
        if (empty($value) && $form_state
          ->getValue($id . '_cancel_enabled')) {
          $form_state
            ->setErrorByName($field, $this
            ->t('You must enter a path in this field'));
          $form['entity_type_cancel_destination'][$id][$id . '_bundles']['#open'] = TRUE;
        }
        elseif (!$this->pathValidator
          ->isValid($value)) {
          $form_state
            ->setErrorByName($field, $this
            ->t("The path '%path' is either invalid or you do not have access to it.", [
            '%path' => $value,
          ]));
          $form['entity_type_cancel_destination'][$id][$id . '_bundles']['#open'] = TRUE;
        }
      }
    }
  }
}