function BookOutlineConstraintValidator::validate

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

File

core/modules/book/src/Plugin/Validation/Constraint/BookOutlineConstraintValidator.php, line 45

Class

BookOutlineConstraintValidator
Constraint validator for changing the book outline in pending revisions.

Namespace

Drupal\book\Plugin\Validation\Constraint

Code

public function validate($entity, Constraint $constraint) {
    if (isset($entity) && !$entity->isNew() && !$entity->isDefaultRevision()) {
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
        $original = $this->bookManager
            ->loadBookLink($entity->id(), FALSE) ?: [
            'bid' => 0,
            'weight' => 0,
        ];
        if (empty($original['pid'])) {
            $original['pid'] = -1;
        }
        if ($entity->book['bid'] != $original['bid']) {
            $this->context
                ->buildViolation($constraint->message)
                ->atPath('book.bid')
                ->setInvalidValue($entity)
                ->addViolation();
        }
        if ($entity->book['pid'] != $original['pid']) {
            $this->context
                ->buildViolation($constraint->message)
                ->atPath('book.pid')
                ->setInvalidValue($entity)
                ->addViolation();
        }
        if ($entity->book['weight'] != $original['weight']) {
            $this->context
                ->buildViolation($constraint->message)
                ->atPath('book.weight')
                ->setInvalidValue($entity)
                ->addViolation();
        }
    }
}

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