You are here

public function CustomLinksForm::validateForm in Simple XML sitemap 4.x

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/CustomLinksForm.php, line 118

Class

CustomLinksForm
Class CustomLinksForm

Namespace

Drupal\simple_sitemap\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  if (!empty($form_state
    ->getValue('custom_links')) && empty($form_state
    ->getValue('variants'))) {
    $form_state
      ->setErrorByName('variants', $this
      ->t('Custom links must be assigned to at least one sitemap variant.'));
  }
  foreach ($this
    ->stringToCustomLinks($form_state
    ->getValue('custom_links')) as $i => $link_config) {
    $placeholders = [
      '@line' => ++$i,
      '@path' => $link_config['path'],
      '@priority' => isset($link_config['priority']) ? $link_config['priority'] : '',
      '@changefreq' => isset($link_config['changefreq']) ? $link_config['changefreq'] : '',
      '@changefreq_options' => implode(', ', FormHelper::getChangefreqOptions()),
    ];

    // Checking if internal path exists.
    if (!(bool) $this->pathValidator
      ->getUrlIfValidWithoutAccessCheck($link_config['path']) || strpos($link_config['path'], '//') !== FALSE) {
      $form_state
        ->setErrorByName('', $this
        ->t('<strong>Line @line</strong>: The path <em>@path</em> does not exist.', $placeholders));
    }

    // Making sure the paths start with a slash.
    if ($link_config['path'][0] !== '/') {
      $form_state
        ->setErrorByName('', $this
        ->t("<strong>Line @line</strong>: The path <em>@path</em> needs to start with a '/'.", $placeholders));
    }

    // Making sure the priority is formatted correctly.
    if (isset($link_config['priority']) && !FormHelper::isValidPriority($link_config['priority'])) {
      $form_state
        ->setErrorByName('', $this
        ->t('<strong>Line @line</strong>: The priority setting <em>@priority</em> for path <em>@path</em> is incorrect. Set the priority from 0.0 to 1.0.', $placeholders));
    }

    // Making sure changefreq is formatted correctly.
    if (isset($link_config['changefreq']) && !FormHelper::isValidChangefreq($link_config['changefreq'])) {
      $form_state
        ->setErrorByName('', $this
        ->t('<strong>Line @line</strong>: The changefreq setting <em>@changefreq</em> for path <em>@path</em> is incorrect. The following are the correct values: <em>@changefreq_options</em>.', $placeholders));
    }
  }
}