class BookOutlineConstraintValidator in Drupal 9
Same name and namespace in other branches
- 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
- class \Drupal\book\Plugin\Validation\Constraint\BookOutlineConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of BookOutlineConstraintValidator
File
- core/
modules/ book/ src/ Plugin/ Validation/ Constraint/ BookOutlineConstraintValidator.php, line 14
Namespace
Drupal\book\Plugin\Validation\ConstraintView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BookOutlineConstraintValidator:: |
protected | property | The book manager. | |
BookOutlineConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
BookOutlineConstraintValidator:: |
public | function | ||
BookOutlineConstraintValidator:: |
public | function | Creates a new BookOutlineConstraintValidator instance. |