Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. LinkAccessConstraintValidator.php

class LinkAccessConstraintValidator

Validates the LinkAccess constraint.

Hierarchy

  • class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
    • class \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements \Drupal\Core\DependencyInjection\ContainerInjectionInterface

Expanded class hierarchy of LinkAccessConstraintValidator

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
     */
    // phpcs:ignore Drupal.NamingConventions.ValidVariableName.LowerCamelName, Drupal.Commenting.VariableComment.Missing
    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) : void {
        if (isset($value)) {
            try {
                $url = $value->getUrl();
            } catch (\InvalidArgumentException) {
                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
ConstraintValidator::$context protected property
ConstraintValidator::formatTypeOf protected function Returns a string representation of the type of the value.
ConstraintValidator::formatValue protected function Returns a string representation of the value.
ConstraintValidator::formatValues protected function Returns a string representation of a list of values.
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
ConstraintValidator::OBJECT_TO_STRING public constant Whether to cast objects with a "__toString()" method to strings.
ConstraintValidator::PRETTY_DATE public constant Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter}
(if it is available) or as RFC-3339 dates ("Y-m-d H:i:s").
LinkAccessConstraintValidator::$current_user protected property
LinkAccessConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LinkAccessConstraintValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
LinkAccessConstraintValidator::__construct public function Constructs an instance of the LinkAccessConstraintValidator class.
RSS feed
Powered by Drupal