3 namespace Drupal\link\Plugin\Validation\Constraint;
5 use Drupal\Core\Session\AccountProxyInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\Validator\Constraint;
9 use Symfony\Component\Validator\ConstraintValidator;
12 * Validates the LinkAccess constraint.
14 class LinkAccessConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
17 * Proxy for the current user account.
19 * @var \Drupal\Core\Session\AccountProxyInterface
21 protected $current_user;
24 * Constructs an instance of the LinkAccessConstraintValidator class.
26 * @param \Drupal\Core\Session\AccountProxyInterface $current_user
27 * The current user account.
29 public function __construct(AccountProxyInterface $current_user) {
30 $this->current_user = $current_user;
36 public static function create(ContainerInterface $container) {
38 $container->get('current_user')
45 public function validate($value, Constraint $constraint) {
48 $url = $value->getUrl();
50 // If the URL is malformed this constraint cannot check access.
51 catch (\InvalidArgumentException $e) {
54 // Disallow URLs if the current user doesn't have the 'link to any page'
55 // permission nor can access this URI.
56 $allowed = $this->current_user->hasPermission('link to any page') || $url->access();
58 $this->context->addViolation($constraint->message, ['@uri' => $value->uri]);