You are here

public function EuCookieComplianceConfigForm::validatePopupLink in EU Cookie Compliance (GDPR Compliance) 8

Same name and namespace in other branches
  1. 2.0.x src/Form/EuCookieComplianceConfigForm.php \Drupal\eu_cookie_compliance\Form\EuCookieComplianceConfigForm::validatePopupLink()

Validates the banner link field.

Parameters

array $element: Element.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

File

src/Form/EuCookieComplianceConfigForm.php, line 1222

Class

EuCookieComplianceConfigForm
Provides settings for eu_cookie_compliance module.

Namespace

Drupal\eu_cookie_compliance\Form

Code

public function validatePopupLink(array $element, FormStateInterface &$form_state) {
  if (empty($element['#value'])) {
    return;
  }
  $input = $element['#value'];
  if (UrlHelper::isExternal($input)) {
    $allowed_protocols = [
      'http',
      'https',
    ];
    if (!in_array(parse_url($input, PHP_URL_SCHEME), $allowed_protocols)) {
      $form_state
        ->setError($element, $this
        ->t('Invalid protocol specified for the %name (valid protocols: %protocols).', [
        '%name' => $element['#title'],
        '%protocols' => implode(', ', $allowed_protocols),
      ]));
    }
    else {
      try {
        Url::fromUri($input);
      } catch (\Exception $exc) {
        $form_state
          ->setError($element, $this
          ->t('Invalid %name (:message).', [
          '%name' => $element['#title'],
          ':message' => $exc
            ->getMessage(),
        ]));
      }
    }
  }
  else {

    // Special case for '<front>'.
    if ($input === '<front>') {
      $input = '/';
    }
    try {
      Url::fromUserInput($input);
    } catch (\Exception $exc) {
      $form_state
        ->setError($element, $this
        ->t('Invalid URL in %name field (:message).', [
        '%name' => $element['#title'],
        ':message' => $exc
          ->getMessage(),
      ]));
    }
  }
}