function LinkExternalProtocolsConstraintValidator::validate
File
-
core/
modules/ link/ src/ Plugin/ Validation/ Constraint/ LinkExternalProtocolsConstraintValidator.php, line 17
Class
- LinkExternalProtocolsConstraintValidator
- Validates the LinkExternalProtocols constraint.
Namespace
Drupal\link\Plugin\Validation\ConstraintCode
public function validate($value, Constraint $constraint) : void {
if (isset($value)) {
try {
/** @var \Drupal\Core\Url $url */
$url = $value->getUrl();
} catch (\InvalidArgumentException) {
return;
}
// Disallow external URLs using untrusted protocols.
if ($url->isExternal() && !in_array(parse_url($url->getUri(), PHP_URL_SCHEME), UrlHelper::getAllowedProtocols())) {
$this->context
->buildViolation($constraint->message, [
'@uri' => $value->uri,
])
->atPath('uri')
->addViolation();
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.