Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / path / src / Plugin / Validation / Constraint / PathAliasConstraintValidator.php
1 <?php
2
3 namespace Drupal\path\Plugin\Validation\Constraint;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\Validator\Constraint;
9 use Symfony\Component\Validator\ConstraintValidator;
10
11 /**
12  * Constraint validator for changing path aliases in pending revisions.
13  */
14 class PathAliasConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
15
16   /**
17    * The entity type manager.
18    *
19    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
20    */
21   private $entityTypeManager;
22
23   /**
24    * Creates a new PathAliasConstraintValidator instance.
25    *
26    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
27    *   The entity type manager.
28    */
29   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
30     $this->entityTypeManager = $entity_type_manager;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('entity_type.manager')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function validate($value, Constraint $constraint) {
46     $entity = !empty($value->getParent()) ? $value->getEntity() : NULL;
47
48     if ($entity && !$entity->isNew() && !$entity->isDefaultRevision()) {
49       /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
50       $original = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
51       if ($value->alias != $original->path->alias) {
52         $this->context->addViolation($constraint->message);
53       }
54     }
55   }
56
57 }