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

Breadcrumb

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

class PathAliasConstraintValidator

Constraint validator for changing path aliases in pending revisions.

Hierarchy

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

Expanded class hierarchy of PathAliasConstraintValidator

File

core/modules/path/src/Plugin/Validation/Constraint/PathAliasConstraintValidator.php, line 14

Namespace

Drupal\path\Plugin\Validation\Constraint
View source
class PathAliasConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    private $entityTypeManager;
    
    /**
     * Creates a new PathAliasConstraintValidator instance.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager) {
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint) : void {
        $entity = !empty($value->getParent()) ? $value->getEntity() : NULL;
        if ($entity && !$entity->isNew() && !$entity->isDefaultRevision()) {
            
            /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
            $original = $this->entityTypeManager
                ->getStorage($entity->getEntityTypeId())
                ->loadUnchanged($entity->id());
            $entity_langcode = $entity->language()
                ->getId();
            // Only add the violation if the current translation does not have the
            // same path alias.
            if ($original->hasTranslation($entity_langcode)) {
                if ($value->alias != $original->getTranslation($entity_langcode)->path->alias) {
                    $this->context
                        ->addViolation($constraint->message);
                }
            }
        }
    }

}

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").
PathAliasConstraintValidator::$entityTypeManager private property The entity type manager.
PathAliasConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
PathAliasConstraintValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
PathAliasConstraintValidator::__construct public function Creates a new PathAliasConstraintValidator instance.
RSS feed
Powered by Drupal