class LinkAccessConstraintValidator in Zircon Profile 8
Same name and namespace in other branches
- 8.0 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 implements ContainerInjectionInterface, ConstraintValidatorInterface
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 - Contains \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkAccessConstraintValidatorTest.
File
- core/
modules/ link/ src/ Plugin/ Validation/ Constraint/ LinkAccessConstraintValidator.php, line 20 - Contains \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator.
Namespace
Drupal\link\Plugin\Validation\ConstraintView source
class LinkAccessConstraintValidator implements ConstraintValidatorInterface, ContainerInjectionInterface {
/**
* Stores the validator's state during validation.
*
* @var \Symfony\Component\Validator\ExecutionContextInterface
*/
protected $context;
/**
* 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 initialize(ExecutionContextInterface $context) {
$this->context = $context;
}
/**
* {@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, array(
'@uri' => $value->uri,
));
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LinkAccessConstraintValidator:: |
protected | property | Stores the validator's state during validation. | |
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 |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
|
LinkAccessConstraintValidator:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |
|
LinkAccessConstraintValidator:: |
public | function | Constructs an instance of the LinkAccessConstraintValidator class. |