You are here

public function SimplesitemapCustomLinksForm::validateForm in Simple XML sitemap 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/SimplesitemapCustomLinksForm.php, line 76
Contains \Drupal\simplesitemap\Form\SimplesitemapCustomLinksForm.

Class

SimplesitemapCustomLinksForm
SimplesitemapCustomLinksFrom

Namespace

Drupal\simplesitemap\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $custom_links_string = str_replace("\r\n", "\n", $form_state
    ->getValue('custom_links'));
  $custom_links = array_filter(explode("\n", $custom_links_string), 'trim');
  foreach ($custom_links as $link_setting) {
    $settings = explode(' ', $link_setting, 2);
    if (!\Drupal::service('path.validator')
      ->isValid($settings[0])) {
      $form_state
        ->setErrorByName('', t("The path <em>{$settings[0]}</em> does not exist."));
    }
    if ($settings[0][0] != '/') {
      $form_state
        ->setErrorByName('', t("The path <em>{$settings[0]}</em> needs to start with an '/'."));
    }
    if (isset($settings[1])) {
      if (!is_numeric($settings[1]) || $settings[1] < 0 || $settings[1] > 1) {
        $form_state
          ->setErrorByName('', t("Priority setting on line <em>{$link_setting}</em> is incorrect. Set priority from 0.0 to 1.0."));
      }
    }
  }
}