diff --git a/core/modules/book/book.module b/core/modules/book/book.module index ee84fb6..71e9d32 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -182,11 +182,7 @@ function book_menu() { ); $items['node/%node/outline/remove'] = array( 'title' => 'Remove from outline', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('book_remove_form', 1), - 'access callback' => '_book_outline_remove_access', - 'access arguments' => array(1), - 'file' => 'book.pages.inc', + 'route_name' => 'book_remove', ); return $items; @@ -219,18 +215,6 @@ function _book_outline_access(EntityInterface $node) { } /** - * Access callback: Determines if the user can remove nodes from the outline. - * - * @param \Drupal\Core\Entity\EntityInterface $node - * The node to remove from the outline. - * - * @see book_menu() - */ -function _book_outline_remove_access(EntityInterface $node) { - return _book_node_is_removable($node) && _book_outline_access($node); -} - -/** * Determines if a node can be removed from the book. * * A node can be removed from a book if it is actually in a book and it either diff --git a/core/modules/book/book.pages.inc b/core/modules/book/book.pages.inc index 67c4c99..af5b072 100644 --- a/core/modules/book/book.pages.inc +++ b/core/modules/book/book.pages.inc @@ -187,42 +187,3 @@ function book_outline_form_submit($form, &$form_state) { drupal_set_message(t('There was an error adding the post to the book.'), 'error'); } } - -/** - * Form constructor to confirm removal of a node from a book. - * - * @param \Drupal\Core\Entity\EntityInterface $node - * The node to delete. - * - * @see book_remove_form_submit() - * @see book_menu() - * @ingroup forms - */ -function book_remove_form($form, &$form_state, EntityInterface $node) { - $form['#node'] = $node; - $title = array('%title' => $node->label()); - - if ($node->book['has_children']) { - $description = t('%title has associated child pages, which will be relocated automatically to maintain their connection to the book. To recreate the hierarchy (as it was before removing this page), %title may be added again using the Outline tab, and each of its former child pages will need to be relocated manually.', $title); - } - else { - $description = t('%title may be added to hierarchy again using the Outline tab.', $title); - } - - return confirm_form($form, t('Are you sure you want to remove %title from the book hierarchy?', $title), 'node/' . $node->nid, $description, t('Remove')); -} - -/** - * Form submission handler for book_remove_form(). - */ -function book_remove_form_submit($form, &$form_state) { - $node = $form['#node']; - if (_book_node_is_removable($node)) { - menu_link_delete($node->book['mlid']); - db_delete('book') - ->condition('nid', $node->nid) - ->execute(); - drupal_set_message(t('The post has been removed from the book.')); - } - $form_state['redirect'] = 'node/' . $node->nid; -} diff --git a/core/modules/book/book.routing.yml b/core/modules/book/book.routing.yml index 77b492b..a303f76 100644 --- a/core/modules/book/book.routing.yml +++ b/core/modules/book/book.routing.yml @@ -18,3 +18,12 @@ book_settings: _form: 'Drupal\book\BookSettingsForm' requirements: _permission: 'administer site configuration' + +book_remove: + pattern: 'node/{node}/outline/remove' + defaults: + _form: '\Drupal\book\Form\BookRemoveForm' + requirements: + _permission: 'administer book outlines' + _node_access: 'view' + _book_node_is_removable: 'TRUE' diff --git a/core/modules/book/book.services.yml b/core/modules/book/book.services.yml index 254c6ec..697063a 100644 --- a/core/modules/book/book.services.yml +++ b/core/modules/book/book.services.yml @@ -2,3 +2,7 @@ services: book.manager: class: Drupal\book\BookManager arguments: ['@database'] + access_check.book.removable: + class: Drupal\book\Access\BookNodeIsRemovableAccessCheck + tags: + - { name: access_check } diff --git a/core/modules/book/lib/Drupal/book/Access/BookNodeIsRemovableAccessCheck.php b/core/modules/book/lib/Drupal/book/Access/BookNodeIsRemovableAccessCheck.php new file mode 100644 index 0000000..c938748 --- /dev/null +++ b/core/modules/book/lib/Drupal/book/Access/BookNodeIsRemovableAccessCheck.php @@ -0,0 +1,36 @@ +getRequirements()); + } + + /** + * {@inheritdoc} + */ + public function access(Route $route, Request $request) { + $node = $request->attributes->get('node'); + $removable = (_book_node_is_removable($node) ? 'TRUE' : 'FALSE'); + if ($removable != strtoupper($route->getRequirement('_book_node_is_removable'))) { + return FALSE; + } + } +} diff --git a/core/modules/book/lib/Drupal/book/Form/BookRemoveForm.php b/core/modules/book/lib/Drupal/book/Form/BookRemoveForm.php new file mode 100644 index 0000000..34e754e --- /dev/null +++ b/core/modules/book/lib/Drupal/book/Form/BookRemoveForm.php @@ -0,0 +1,119 @@ +get('database')); + } + + /** + * Constructs a BookRemoveForm object. + * + * @param \Drupal\Core\Database\Connection $database + * Database connection. + */ + public function __construct(Connection $database) { + $this->database = $database; + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'book_remove_form'; + } + + /** + * {@inheritdoc} + */ + protected function getCancelPath() { + return 'node/' . $this->node->id(); + } + + /** + * {@inheritdoc} + */ + protected function getQuestion() { + return t('Are you sure you want to remove %title from the book hierarchy?', array('%title' => $this->node->label())); + } + + /** + * {@inheritdoc} + */ + protected function getDescription() { + $t_title = array('%title' => $this->node->label()); + if ($this->node->book['has_children']) { + $description = t('%title has associated child pages, which will be relocated automatically to maintain their connection to the book. To recreate the hierarchy (as it was before removing this page), %title may be added again using the Outline tab, and each of its former child pages will need to be relocated manually.', $t_title); + } + else { + $description = t('%title may be added to hierarchy again using the Outline tab.', $t_title); + } + return $description; + } + + /** + * {@inheritdoc} + */ + protected function getConfirmText() { + return 'Remove'; + } + + /** + * {@inheritdoc} + * + * @param \Drupal\node\NodeInterface $node + * The node to delete. + */ + public function buildForm(array $form, array &$form_state, NodeInterface $node = NULL) { + $this->node = $node; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $node = $form['#node']; + if (_book_node_is_removable($node)) { + menu_link_delete($node->book['mlid']); + $this->database->delete('book') + ->condition('nid', $node->nid) + ->execute(); + drupal_set_message(t('The post has been removed from the book.')); + } + $form_state['redirect'] = 'node/' . $node->nid; + } + +} diff --git a/core/modules/node/lib/Drupal/node/Access/NodeAccessCheck.php b/core/modules/node/lib/Drupal/node/Access/NodeAccessCheck.php new file mode 100644 index 0000000..1e0a327 --- /dev/null +++ b/core/modules/node/lib/Drupal/node/Access/NodeAccessCheck.php @@ -0,0 +1,36 @@ +getRequirements()); + } + + /** + * {@inheritdoc} + */ + public function access(Route $route, Request $request) { + $node = $request->attributes->get('node'); + $op = $route->getRequirement('_node_access'); + if (!node_access($op, $node)) { + return FALSE; + } + } +} diff --git a/core/modules/node/node.services.yml b/core/modules/node/node.services.yml new file mode 100644 index 0000000..947443d --- /dev/null +++ b/core/modules/node/node.services.yml @@ -0,0 +1,5 @@ +services: + access_check.node.node_access: + class: Drupal\node\Access\NodeAccessCheck + tags: + - { name: access_check }