You are here

public function XmlSitemapCustomEditForm::validateForm in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 xmlsitemap_custom/src/Form/XmlSitemapCustomEditForm.php \Drupal\xmlsitemap_custom\Form\XmlSitemapCustomEditForm::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

xmlsitemap_custom/src/Form/XmlSitemapCustomEditForm.php, line 173

Class

XmlSitemapCustomEditForm
Provides a form for editing a custom link.

Namespace

Drupal\xmlsitemap_custom\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $link = $form_state
    ->getValues();
  if (strpos($link['loc'], '/') !== 0) {
    $form_state
      ->setErrorByName('loc', $this
      ->t('The path should start with /.'));
    return;
  }

  // Make sure we trim and normalize the path first.
  $link['loc'] = trim($link['loc']);
  $link['loc'] = $this->aliasManager
    ->getPathByAlias($link['loc'], $link['language']);
  $form_state
    ->setValue('loc', $link['loc']);
  try {
    $client = $this->httpClientFactory
      ->fromOptions([
      'config/curl',
      [
        CURLOPT_FOLLOWLOCATION => FALSE,
      ],
    ]);
    $client
      ->get(Url::fromUserInput($link['loc'], [
      'absolute' => TRUE,
    ])
      ->toString());
  } catch (\Exception $e) {
    $form_state
      ->setErrorByName('loc', $this
      ->t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', [
      '@link' => $link['loc'],
    ]));
  }
  parent::validateForm($form, $form_state);
}