You are here

public static function IframeWidgetBase::validateUrl in Iframe 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/IframeWidgetBase.php \Drupal\iframe\Plugin\Field\FieldWidget\IframeWidgetBase::validateUrl()

Validate url.

See also

\Drupal\Core\Form\FormValidator

File

src/Plugin/Field/FieldWidget/IframeWidgetBase.php, line 338

Class

IframeWidgetBase
Plugin implementation base functions.

Namespace

Drupal\iframe\Plugin\Field\FieldWidget

Code

public static function validateUrl(&$form, FormStateInterface &$form_state) {
  $parents = $form['#parents'];
  $itemfield = $parents[0];
  $iteminst = $parents[1];

  /*
   * $value = $form['#value'];
   * $itemname = $parents[2];
   * $itemid = $form['#id'];
   */
  $node = $form_state
    ->getUserInput();
  $me = $node[$itemfield][$iteminst];
  $testabsolute = true;

  // \iframe_debug(0, 'validateUrl', $me);
  if (!empty($me['url'])) {
    if (preg_match('#^/($|[^/])#', $me['url'])) {
      $testabsolute = false;
    }
    if (!UrlHelper::isValid($me['url'], $testabsolute)) {
      $form_state
        ->setError($form, t('Invalid syntax for "Iframe URL".'));
    }
    elseif (strpos($me['url'], '//') === 0) {
      $form_state
        ->setError($form, t('Drupal does not accept scheme-less URLs. Please add "https:" to your URL, this works on http-parent-pages too.'));
    }
  }
}