class LinkAccessConstraintValidator

Same name in other branches
  1. 8.9.x core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator
  2. 10 core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator
  3. 11.x core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator

Validates the LinkAccess constraint.

Hierarchy

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\Constraint
View 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

Title Sort descending Modifiers Object type Summary Overriden Title
LinkAccessConstraintValidator::$current_user protected property Proxy for the current user account.
LinkAccessConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LinkAccessConstraintValidator::validate public function
LinkAccessConstraintValidator::__construct public function Constructs an instance of the LinkAccessConstraintValidator class.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.