You are here

public function SettingsForm::validateForm in External Links Filter 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/SettingsForm.php, line 68

Class

SettingsForm
Defines a form that configures external link filter settings.

Namespace

Drupal\elf\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $values = preg_split('#\\s#', $values['elf_domains']);
  $domains = [];
  $errors = [];
  foreach ($values as $value) {

    // Remove trailing slashes, because not all users will use those for their links.
    $value = trim($value, '/');
    if (strlen($value)) {
      if (!UrlHelper::isExternal($value)) {
        $errors[] = $value;
      }
      $domains[] = $value;
    }
  }
  if ($errors) {
    $form_state
      ->setErrorByName('elf_domains', $this
      ->formatPlural(count($errors), '%domain is not a valid external domain.', '%domain are no valid external domains', [
      '%domain' => implode(', ', $errors),
    ]));
  }
  else {
    $form_state
      ->setValue('elf_domains', array_unique($domains));
  }
}