You are here

public function BookOutlineConstraintValidator::validate in Drupal 8

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