You are here

public function LinkAccessConstraintValidator::validate in Drupal 9

Same name and namespace in other branches
  1. 8 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,
      ]);
    }
  }
}