class LinkAccessConstraintValidator in Drupal 8
Same name and namespace in other branches
- 9 core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator
Validates the LinkAccess constraint.
Hierarchy
- class \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of LinkAccessConstraintValidator
1 file declares its use of LinkAccessConstraintValidator
- LinkAccessConstraintValidatorTest.php in core/
modules/ link/ tests/ src/ Unit/ Plugin/ Validation/ Constraint/ LinkAccessConstraintValidatorTest.php
File
- core/
modules/ link/ src/ Plugin/ Validation/ Constraint/ LinkAccessConstraintValidator.php, line 14
Namespace
Drupal\link\Plugin\Validation\ConstraintView source
class LinkAccessConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* Proxy for the current user account.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $current_user;
/**
* Constructs an instance of the LinkAccessConstraintValidator class.
*
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user account.
*/
public function __construct(AccountProxyInterface $current_user) {
$this->current_user = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('current_user'));
}
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
if (isset($value)) {
try {
$url = $value
->getUrl();
} catch (\InvalidArgumentException $e) {
return;
}
// Disallow URLs if the current user doesn't have the 'link to any page'
// permission nor can access this URI.
$allowed = $this->current_user
->hasPermission('link to any page') || $url
->access();
if (!$allowed) {
$this->context
->addViolation($constraint->message, [
'@uri' => $value->uri,
]);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LinkAccessConstraintValidator:: |
protected | property | Proxy for the current user account. | |
LinkAccessConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
LinkAccessConstraintValidator:: |
public | function | Checks if the passed value is valid. | |
LinkAccessConstraintValidator:: |
public | function | Constructs an instance of the LinkAccessConstraintValidator class. |