Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / book / src / Plugin / Validation / Constraint / BookOutlineConstraintValidator.php
1 <?php
2
3 namespace Drupal\book\Plugin\Validation\Constraint;
4
5 use Drupal\book\BookManagerInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
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 the book outline in pending revisions.
13  */
14 class BookOutlineConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
15
16   /**
17    * The book manager.
18    *
19    * @var \Drupal\book\BookManagerInterface
20    */
21   protected $bookManager;
22
23   /**
24    * Creates a new BookOutlineConstraintValidator instance.
25    *
26    * @param \Drupal\book\BookManagerInterface $book_manager
27    *   The book manager.
28    */
29   public function __construct(BookManagerInterface $book_manager) {
30     $this->bookManager = $book_manager;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('book.manager')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function validate($entity, Constraint $constraint) {
46     if (isset($entity) && !$entity->isNew() && !$entity->isDefaultRevision()) {
47       /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
48       $original = $this->bookManager->loadBookLink($entity->id(), FALSE) ?: [
49         'bid' => 0,
50         'weight' => 0,
51       ];
52       if (empty($original['pid'])) {
53         $original['pid'] = -1;
54       }
55
56       if ($entity->book['bid'] != $original['bid']) {
57         $this->context->buildViolation($constraint->message)
58           ->atPath('book.bid')
59           ->setInvalidValue($entity)
60           ->addViolation();
61       }
62       if ($entity->book['pid'] != $original['pid']) {
63         $this->context->buildViolation($constraint->message)
64           ->atPath('book.pid')
65           ->setInvalidValue($entity)
66           ->addViolation();
67       }
68       if ($entity->book['weight'] != $original['weight']) {
69         $this->context->buildViolation($constraint->message)
70           ->atPath('book.weight')
71           ->setInvalidValue($entity)
72           ->addViolation();
73       }
74     }
75   }
76
77 }