You are here

public function SimplesitemapCustomLinksForm::validateForm in Simple XML sitemap 8.3

Same name and namespace in other branches
  1. 8.2 src/Form/SimplesitemapCustomLinksForm.php \Drupal\simple_sitemap\Form\SimplesitemapCustomLinksForm::validateForm()

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 111

Class

SimplesitemapCustomLinksForm
Class SimplesitemapCustomLinksForm @package Drupal\simple_sitemap\Form

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));
    }
  }
}