class LinkExternalProtocolsConstraintValidator in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator
Validates the LinkExternalProtocols constraint.
Hierarchy
- class \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator implements ConstraintValidatorInterface
Expanded class hierarchy of LinkExternalProtocolsConstraintValidator
1 file declares its use of LinkExternalProtocolsConstraintValidator
- LinkExternalProtocolsConstraintValidatorTest.php in core/
modules/ link/ tests/ src/ Unit/ Plugin/ Validation/ Constraint/ LinkExternalProtocolsConstraintValidatorTest.php - Contains \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidatorTest.
File
- core/
modules/ link/ src/ Plugin/ Validation/ Constraint/ LinkExternalProtocolsConstraintValidator.php, line 18 - Contains \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator.
Namespace
Drupal\link\Plugin\Validation\ConstraintView source
class LinkExternalProtocolsConstraintValidator implements ConstraintValidatorInterface {
/**
* Stores the validator's state during validation.
*
* @var \Symfony\Component\Validator\ExecutionContextInterface
*/
protected $context;
/**
* {@inheritdoc}
*/
public function initialize(ExecutionContextInterface $context) {
$this->context = $context;
}
/**
* {@inheritdoc}
*/
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,
));
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LinkExternalProtocolsConstraintValidator:: |
protected | property | Stores the validator's state during validation. | |
LinkExternalProtocolsConstraintValidator:: |
public | function |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
|
LinkExternalProtocolsConstraintValidator:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |