public function XmlSitemapCustomAddForm::validateForm in XML sitemap 8
Same name and namespace in other branches
- 2.x xmlsitemap_custom/src/Form/XmlSitemapCustomAddForm.php \Drupal\xmlsitemap_custom\Form\XmlSitemapCustomAddForm::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/ XmlSitemapCustomAddForm.php, line 201
Class
- XmlSitemapCustomAddForm
- Provides a form for adding a custom link.
Namespace
Drupal\xmlsitemap_custom\FormCode
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']);
$query = $this->connection
->select('xmlsitemap');
$query
->fields('xmlsitemap');
$query
->condition('type', 'custom');
$query
->condition('loc', $link['loc']);
$query
->condition('status', 1);
$query
->condition('access', 1);
$query
->condition('language', $link['language']);
$result = $query
->execute()
->fetchAssoc();
if ($result != FALSE) {
$form_state
->setErrorByName('loc', $this
->t('There is already an existing link in the sitemap with the path %link.', [
'%link' => $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);
}