You are here

public function LinkExternalProtocolsConstraintValidator::validate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator::validate()

Checks if the passed value is valid.

Parameters

mixed $value The value that should be validated:

Constraint $constraint The constraint for the validation:

Overrides ConstraintValidatorInterface::validate

File

core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php, line 37
Contains \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator.

Class

LinkExternalProtocolsConstraintValidator
Validates the LinkExternalProtocols constraint.

Namespace

Drupal\link\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {
  if (isset($value)) {
    try {

      /** @var \Drupal\Core\Url $url */
      $url = $value
        ->getUrl();
    } catch (\InvalidArgumentException $e) {
      return;
    }

    // Disallow external URLs using untrusted protocols.
    if ($url
      ->isExternal() && !in_array(parse_url($url
      ->getUri(), PHP_URL_SCHEME), UrlHelper::getAllowedProtocols())) {
      $this->context
        ->addViolation($constraint->message, array(
        '@uri' => $value->uri,
      ));
    }
  }
}