function LinkAccessConstraintValidator::validate

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

File

core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php, line 45

Class

LinkAccessConstraintValidator
Validates the LinkAccess constraint.

Namespace

Drupal\link\Plugin\Validation\Constraint

Code

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,
            ]);
        }
    }
}

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