PathAliasConstraintValidator.php in Drupal 8
File
core/modules/path/src/Plugin/Validation/Constraint/PathAliasConstraintValidator.php
View source
<?php
namespace Drupal\path\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class PathAliasConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
private $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function validate($value, Constraint $constraint) {
$entity = !empty($value
->getParent()) ? $value
->getEntity() : NULL;
if ($entity && !$entity
->isNew() && !$entity
->isDefaultRevision()) {
$original = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->loadUnchanged($entity
->id());
$entity_langcode = $entity
->language()
->getId();
if ($original
->hasTranslation($entity_langcode)) {
if ($value->alias != $original
->getTranslation($entity_langcode)->path->alias) {
$this->context
->addViolation($constraint->message);
}
}
}
}
}