You are here

class BookOutlineConstraintValidator in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/book/src/Plugin/Validation/Constraint/BookOutlineConstraintValidator.php \Drupal\book\Plugin\Validation\Constraint\BookOutlineConstraintValidator

Constraint validator for changing the book outline in pending revisions.

Hierarchy

Expanded class hierarchy of BookOutlineConstraintValidator

File

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

Namespace

Drupal\book\Plugin\Validation\Constraint
View source
class BookOutlineConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * The book manager.
   *
   * @var \Drupal\book\BookManagerInterface
   */
  protected $bookManager;

  /**
   * Creates a new BookOutlineConstraintValidator instance.
   *
   * @param \Drupal\book\BookManagerInterface $book_manager
   *   The book manager.
   */
  public function __construct(BookManagerInterface $book_manager) {
    $this->bookManager = $book_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('book.manager'));
  }

  /**
   * {@inheritdoc}
   */
  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();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BookOutlineConstraintValidator::$bookManager protected property The book manager.
BookOutlineConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
BookOutlineConstraintValidator::validate public function
BookOutlineConstraintValidator::__construct public function Creates a new BookOutlineConstraintValidator instance.