function PathAliasConstraintValidator::validate

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

File

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

Class

PathAliasConstraintValidator
Constraint validator for changing path aliases in pending revisions.

Namespace

Drupal\path\Plugin\Validation\Constraint

Code

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

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