diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 26f324e..b59da77 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -176,13 +176,13 @@ function book_menu() { ); $items['node/%node/outline'] = array( 'title' => 'Outline', - 'page callback' => 'book_outline', - 'page arguments' => array(1), - 'access callback' => '_book_outline_access', - 'access arguments' => array(1), + 'route' => 'book_outline', 'type' => MENU_LOCAL_TASK, 'weight' => 2, - 'file' => 'book.pages.inc', + // @TODO: is menu access independent of route access? Local Task + // is not showing up without access callback set here. + 'access callback' => '_book_outline_access', + 'access arguments' => array(1), ); $items['node/%node/outline/remove'] = array( 'title' => 'Remove from outline', diff --git a/core/modules/book/book.pages.inc b/core/modules/book/book.pages.inc index 2418591..4077ad6 100644 --- a/core/modules/book/book.pages.inc +++ b/core/modules/book/book.pages.inc @@ -110,6 +110,8 @@ function book_export_html(EntityInterface $node) { * A HTML-formatted string with the outline form for a single node. * * @see book_menu() + * + * @TODO: figure out how to handle set title for D8 routing */ function book_outline(EntityInterface $node) { drupal_set_title($node->label()); @@ -117,95 +119,19 @@ function book_outline(EntityInterface $node) { } /** - * Form constructor for the book outline form. - * - * Allows handling of all book outline operations via the outline tab. - * - * @param \Drupal\Core\Entity\EntityInterface $node - * The book node for which to show the outline. - * - * @see book_outline_form_submit() - * @see book_remove_button_submit() - * @ingroup forms - */ -function book_outline_form($form, &$form_state, EntityInterface $node) { - if (!isset($node->book)) { - // The node is not part of any book yet - set default options. - $node->book = _book_link_defaults($node->nid); - } - else { - $node->book['original_bid'] = $node->book['bid']; - } - - // Find the depth limit for the parent select. - if (!isset($node->book['parent_depth_limit'])) { - $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book); - } - $form['#node'] = $node; - $form['#id'] = 'book-outline'; - _book_add_form_elements($form, $form_state, $node); - - $form['update'] = array( - '#type' => 'submit', - '#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'), - '#weight' => 15, - ); - - $form['remove'] = array( - '#type' => 'submit', - '#value' => t('Remove from book outline'), - '#access' => _book_node_is_removable($node), - '#weight' => 20, - '#submit' => array('book_remove_button_submit'), - ); - - return $form; -} - -/** * Form submission handler for book_outline_form(). * * Redirects to removal confirmation form. * * @see book_outline_form_submit() + * + * @TODO: should refactor to BookOutlineForm method? */ function book_remove_button_submit($form, &$form_state) { $form_state['redirect'] = 'node/' . $form['#node']->nid . '/outline/remove'; } /** - * Form submission handler for book_outline_form(). - * - * @see book_remove_button_submit() - */ -function book_outline_form_submit($form, &$form_state) { - $node = $form['#node']; - $form_state['redirect'] = "node/" . $node->nid; - $book_link = $form_state['values']['book']; - if (!$book_link['bid']) { - drupal_set_message(t('No changes were made')); - - return; - } - - $book_link['menu_name'] = book_menu_name($book_link['bid']); - $node->book = $book_link; - if (_book_update_outline($node)) { - if ($node->book['parent_mismatch']) { - // This will usually only happen when JS is disabled. - drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.')); - $form_state['redirect'] = "node/" . $node->nid . "/outline"; - } - else { - drupal_set_message(t('The book outline has been updated.')); - } - } - else { - 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 diff --git a/core/modules/book/book.routing.yml b/core/modules/book/book.routing.yml index 3af7c08..e52cdfd 100644 --- a/core/modules/book/book.routing.yml +++ b/core/modules/book/book.routing.yml @@ -4,3 +4,10 @@ book_settings: _form: 'Drupal\book\BookSettingsForm' requirements: _permission: 'administer site configuration' + +book_outline: + pattern: 'node/{node}/outline' + defaults: + _form: '\Drupal\book\Form\BookOutlineForm' + requirements: + _permission: 'administer book outlines' diff --git a/core/modules/book/lib/Drupal/book/Form/BookOutlineForm.php b/core/modules/book/lib/Drupal/book/Form/BookOutlineForm.php new file mode 100644 index 0000000..120afcd --- /dev/null +++ b/core/modules/book/lib/Drupal/book/Form/BookOutlineForm.php @@ -0,0 +1,129 @@ +get('database')); + } + + public function getFormID() { + return 'book_outline'; + } + + /** + * @TODO: update docblock copied from book_outline_form() + * Form constructor for the book outline form. + * + * Allows handling of all book outline operations via the outline tab. + * + * @param Drupal\node\Node $node + * The book node for which to show the outline. + * + * @see book_outline_form_submit() + * @see book_remove_button_submit() + * @ingroup forms + */ + public function buildForm(array $form, array &$form_state, Node $node = NULL) { + if (!isset($node->book)) { + // The node is not part of any book yet - set default options. + $node->book = _book_link_defaults($node->nid); + } + else { + $node->book['original_bid'] = $node->book['bid']; + } + + // Find the depth limit for the parent select. + if (!isset($node->book['parent_depth_limit'])) { + $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book); + } + $form['#node'] = $node; + $form['#id'] = 'book-outline'; + _book_add_form_elements($form, $form_state, $node); + + $form['update'] = array( + '#type' => 'submit', + '#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'), + '#weight' => 15, + ); + + $form['remove'] = array( + '#type' => 'submit', + '#value' => t('Remove from book outline'), + '#access' => _book_node_is_removable($node), + '#weight' => 20, + '#submit' => array('book_remove_button_submit'), + ); + + return $form; + } + + /** + * [validateForm description] + * @param array $form [description] + * @param array $form_state [description] + * @return [type] [description] + * + * @TODO: null implementation or declare class as abstract? + */ + public function validateForm(array &$form, array &$form_state) { + // ... + } + + /** + * @TODO: update docblock copied from book_outline_form_submit() + * Form submission handler for book_outline_form(). + * + * @see book_remove_button_submit() + */ + public function submitForm(array &$form, array &$form_state) { + $node = $form['#node']; + $form_state['redirect'] = "node/" . $node->nid; + $book_link = $form_state['values']['book']; + if (!$book_link['bid']) { + drupal_set_message(t('No changes were made')); + + return; + } + + $book_link['menu_name'] = book_menu_name($book_link['bid']); + $node->book = $book_link; + if (_book_update_outline($node)) { + if ($node->book['parent_mismatch']) { + // This will usually only happen when JS is disabled. + drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.')); + $form_state['redirect'] = "node/" . $node->nid . "/outline"; + } + else { + drupal_set_message(t('The book outline has been updated.')); + } + } + else { + drupal_set_message(t('There was an error adding the post to the book.'), 'error'); + } + } +}