LinkExternalProtocolsConstraintValidator.php in Drupal 9
File
core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php
View source
<?php
namespace Drupal\link\Plugin\Validation\Constraint;
use Drupal\Component\Utility\UrlHelper;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class LinkExternalProtocolsConstraintValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if (isset($value)) {
try {
$url = $value
->getUrl();
} catch (\InvalidArgumentException $e) {
return;
}
if ($url
->isExternal() && !in_array(parse_url($url
->getUri(), PHP_URL_SCHEME), UrlHelper::getAllowedProtocols())) {
$this->context
->addViolation($constraint->message, [
'@uri' => $value->uri,
]);
}
}
}
}