You are here

public function PathAliasConstraintValidator::validate in Drupal 8

Same name and namespace in other branches
  1. 9 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) {
  $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);
      }
    }
  }
}