diff --git a/core/modules/book/book.module b/core/modules/book/book.module index d0aa9b0..4c62274 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -82,7 +82,8 @@ function book_entity_type_build(array &$entity_types) { $entity_types['node'] ->setFormClass('book_outline', 'Drupal\book\Form\BookOutlineForm') ->setLinkTemplate('book-outline-form', '/node/{node}/outline') - ->setLinkTemplate('book-remove-form', '/node/{node}/outline/remove'); + ->setLinkTemplate('book-remove-form', '/node/{node}/outline/remove') + ->addConstraint('BookOutline', []); } /** diff --git a/core/modules/book/src/BookManager.php b/core/modules/book/src/BookManager.php index 03ff784..402776c 100644 --- a/core/modules/book/src/BookManager.php +++ b/core/modules/book/src/BookManager.php @@ -278,36 +278,8 @@ public function updateOutline(NodeInterface $node) { // Prevent changes to the book outline if the node being saved is not the // default revision. - // @todo Convert this to an entity constraint in - // https://www.drupal.org/node/2883868. $updated = !$new && (($original = $this->loadBookLink($node->id(), FALSE)) && ($node->book['bid'] != $original['bid'] || $node->book['pid'] != $original['pid'])); if (($new || $updated) && !$node->isDefaultRevision()) { - drupal_set_message($this->t('This is not the default revision. You can only change the book outline for the published version of this content.'), 'error'); - - // Improve the usability of the error message by showing the changes that - // could not be applied. - if ($updated) { - $changes = []; - $new_parents = $this->loadBookLinks([$node->book['bid'], $node->book['pid']]); - if ($node->book['bid'] != $original['bid'] && $new_parents[$node->book['bid']]['access']) { - $changes[] = $this->t('The book was not changed to %book_label', ['%book_label' => $new_parents[$node->book['bid']]['title']]); - } - if ($node->book['pid'] != $original['pid'] && $new_parents[$node->book['pid']]['access']) { - $changes[] = $this->t('The parent was not changed to %parent_label', ['%parent_label' => $new_parents[$node->book['pid']]['title']]); - } - - $message = [ - [ - '#markup' => $this->t('The following changes were not applied:') - ], - [ - '#theme' => 'item_list', - '#items' => $changes, - ] - ]; - drupal_set_message($this->renderer->renderPlain($message), 'warning'); - } - return FALSE; } diff --git a/core/modules/book/src/Plugin/Validation/Constraint/BookOutlineConstraint.php b/core/modules/book/src/Plugin/Validation/Constraint/BookOutlineConstraint.php new file mode 100644 index 0000000..677d36c --- /dev/null +++ b/core/modules/book/src/Plugin/Validation/Constraint/BookOutlineConstraint.php @@ -0,0 +1,19 @@ +published version of this content.'; + +} diff --git a/core/modules/book/src/Plugin/Validation/Constraint/BookOutlineConstraintValidator.php b/core/modules/book/src/Plugin/Validation/Constraint/BookOutlineConstraintValidator.php new file mode 100644 index 0000000..66ff644 --- /dev/null +++ b/core/modules/book/src/Plugin/Validation/Constraint/BookOutlineConstraintValidator.php @@ -0,0 +1,57 @@ +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); + if ($entity->book['bid'] != $original['bid'] || $entity->book['pid'] != $original['pid']) { + $this->context->buildViolation($constraint->message) + ->setInvalidValue($entity) + ->addViolation(); + } + } + } + +}