3 namespace Drupal\link\Plugin\Validation\Constraint;
5 use Drupal\Component\Utility\UrlHelper;
6 use Symfony\Component\Validator\Constraint;
7 use Symfony\Component\Validator\ConstraintValidator;
10 * Validates the LinkExternalProtocols constraint.
12 class LinkExternalProtocolsConstraintValidator extends ConstraintValidator {
17 public function validate($value, Constraint $constraint) {
20 /** @var \Drupal\Core\Url $url */
21 $url = $value->getUrl();
23 // If the URL is malformed this constraint cannot check further.
24 catch (\InvalidArgumentException $e) {
27 // Disallow external URLs using untrusted protocols.
28 if ($url->isExternal() && !in_array(parse_url($url->getUri(), PHP_URL_SCHEME), UrlHelper::getAllowedProtocols())) {
29 $this->context->addViolation($constraint->message, ['@uri' => $value->uri]);