Unverified Commit 2ddf03f0 authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #3035119 by thalles, Dave Reid: Use strpos() instead of deprecated Unicode::substr().

parent 58521d4e
......@@ -2,19 +2,16 @@
namespace Drupal\xmlsitemap_custom\Form;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Http\ClientFactory;
use Drupal\Core\Language\LanguageInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\xmlsitemap\XmlSitemapLinkStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for adding a custom link.
......@@ -204,7 +201,7 @@ class XmlSitemapCustomAddForm extends FormBase {
public function validateForm(array &$form, FormStateInterface $form_state) {
$link = $form_state->getValues();
if (Unicode::substr($link['loc'], 0, 1) !== '/') {
if (strpos($link['loc'], '/') !== 0) {
$form_state->setErrorByName('loc', $this->t('The path should start with /.'));
return;
}
......@@ -230,10 +227,7 @@ class XmlSitemapCustomAddForm extends FormBase {
$client = $this->httpClientFactory->fromOptions(['config/curl', [CURLOPT_FOLLOWLOCATION => FALSE]]);
$client->get(Url::fromUserInput($link['loc'], ['absolute' => TRUE])->toString());
}
catch (ClientException $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']]));
}
catch (ConnectException $e) {
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']]));
}
......@@ -247,7 +241,7 @@ class XmlSitemapCustomAddForm extends FormBase {
$form_state->cleanValues();
$link = $form_state->getValues();
$this->linkStorage->save($link);
drupal_set_message($this->t('The custom link for %loc was saved.', ['%loc' => $link['loc']]));
$this->messenger()->addMessage($this->t('The custom link for %loc was saved.', ['%loc' => $link['loc']]));
$form_state->setRedirect('xmlsitemap_custom.list');
}
......
......@@ -2,18 +2,17 @@
namespace Drupal\xmlsitemap_custom\Form;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Http\ClientFactory;
use Drupal\Core\Language\LanguageInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\xmlsitemap\XmlSitemapLinkStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\xmlsitemap\XmlSitemapLinkStorageInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for editing a custom link.
......@@ -102,7 +101,7 @@ class XmlSitemapCustomEditForm extends FormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state, $link = '') {
if (!$custom_link = $this->linkStorage->load('custom', $link)) {
drupal_set_message($this->t('No valid custom link specified.'), 'error');
$this->messenger()->addError($this->t('No valid custom link specified.'));
$this->redirect('xmlsitemap_custom.list');
}
else {
......@@ -176,7 +175,7 @@ class XmlSitemapCustomEditForm extends FormBase {
public function validateForm(array &$form, FormStateInterface $form_state) {
$link = $form_state->getValues();
if (Unicode::substr($link['loc'], 0, 1) !== '/') {
if (strpos($link['loc'], '/') !== 0) {
$form_state->setErrorByName('loc', $this->t('The path should start with /.'));
return;
}
......@@ -190,10 +189,7 @@ class XmlSitemapCustomEditForm extends FormBase {
$client = $this->httpClientFactory->fromOptions(['config/curl', [CURLOPT_FOLLOWLOCATION => FALSE]]);
$client->get(Url::fromUserInput($link['loc'], ['absolute' => TRUE])->toString());
}
catch (ClientException $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']]));
}
catch (ConnectException $e) {
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']]));
}
......@@ -207,7 +203,7 @@ class XmlSitemapCustomEditForm extends FormBase {
$form_state->cleanValues();
$link = $form_state->getValues();
$this->linkStorage->save($link);
drupal_set_message($this->t('The custom link for %loc was saved.', ['%loc' => $link['loc']]));
$this->messenger()->addMessage($this->t('The custom link for %loc was saved.', ['%loc' => $link['loc']]));
$form_state->setRedirect('xmlsitemap_custom.list');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment