diff --git a/core/modules/book/book.module b/core/modules/book/book.module index db5b697..7248352 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -351,59 +351,6 @@ function book_pick_book_nojs_submit($form, &$form_state) { } /** - * Builds the parent selection form element for the node form or outline tab. - * - * This function is also called when generating a new set of options during the - * Ajax callback, so an array is returned that can be used to replace an - * existing form element. - * - * @param $book_link - * A fully loaded menu link that is part of the book hierarchy. - * - * @return - * A parent selection form element. - */ -function _book_parent_select($book_link) { - if (Drupal::config('menu.settings')->get('override_parent_selector')) { - return array(); - } - // Offer a message or a drop-down to choose a different parent page. - $form = array( - '#type' => 'hidden', - '#value' => -1, - '#prefix' => '
', - '#suffix' => '
', - ); - - if ($book_link['nid'] === $book_link['bid']) { - // This is a book - at the top level. - if ($book_link['original_bid'] === $book_link['bid']) { - $form['#prefix'] .= '' . t('This is the top-level page in this book.') . ''; - } - else { - $form['#prefix'] .= '' . t('This will be the top-level page in this book.') . ''; - } - } - elseif (!$book_link['bid']) { - $form['#prefix'] .= '' . t('No book selected.') . ''; - } - else { - $form = array( - '#type' => 'select', - '#title' => t('Parent item'), - '#default_value' => $book_link['plid'], - '#description' => t('The parent page in the book. The maximum depth for a book and all child pages is !maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)), - '#options' => book_toc($book_link['bid'], $book_link['parent_depth_limit'], array($book_link['mlid'])), - '#attributes' => array('class' => array('book-title-select')), - '#prefix' => '
', - '#suffix' => '
', - ); - } - - return $form; -} - -/** * Renders a new parent page select element when the book selection changes. * * This function is called via Ajax when the selected book is changed on a node diff --git a/core/modules/book/book.services.yml b/core/modules/book/book.services.yml index a5f9d5a..64e560e 100644 --- a/core/modules/book/book.services.yml +++ b/core/modules/book/book.services.yml @@ -1,4 +1,4 @@ services: book.manager: class: Drupal\book\BookManager - arguments: ['@database', '@plugin.manager.entity', '@string_translation'] + arguments: ['@database', '@plugin.manager.entity', '@string_translation', '@config.factory'] diff --git a/core/modules/book/lib/Drupal/book/BookManager.php b/core/modules/book/lib/Drupal/book/BookManager.php index 5b89ea3..237ab6f 100644 --- a/core/modules/book/lib/Drupal/book/BookManager.php +++ b/core/modules/book/lib/Drupal/book/BookManager.php @@ -11,8 +11,8 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\Config\ConfigFactory; use Drupal\node\NodeInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Book Manager Service. @@ -41,6 +41,13 @@ class BookManager { protected $translation; /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $configFactory; + + /** * Books Array. * * @var array @@ -50,10 +57,11 @@ class BookManager { /** * Constructs a BookManager object. */ - public function __construct(Connection $connection, EntityManager $entityManager, TranslationInterface $translation) { + public function __construct(Connection $connection, EntityManager $entityManager, TranslationInterface $translation, ConfigFactory $configFactory) { $this->connection = $connection; $this->entityManager = $entityManager; $this->translation = $translation; + $this->ConfigFactory = $configFactory; } /** @@ -134,7 +142,7 @@ public function linkDefaults($nid) { /** * Finds the depth limit for items in the parent select. * - * @param $book_link + * @param array $book_link * A fully loaded menu link that is part of the book hierarchy. * * @return int @@ -186,7 +194,7 @@ public function addFormElements(array $form, array &$form_state, NodeInterface $ ); } - $form['book']['plid'] = _book_parent_select($node->book); + $form['book']['plid'] = $this->addParentSelectFormElements($node->book); // @see _book_admin_table_tree(). The weight may be larger than 15. $form['book']['weight'] = array( @@ -346,12 +354,13 @@ public function updateOutline(NodeInterface $node) { * * @return string * The translated string. + * + * @todo Remove when https://drupal.org/node/2018411 is in. */ protected function t($string, array $args = array(), array $options = array()) { return $this->translation->translate($string, $args, $options); } - /** * Generates the corresponding menu name from a book ID. * @@ -368,7 +377,7 @@ public function createMenuName($id) { /** * Updates the book ID of a page and its children when it moves to a new book. * - * @param $book_link + * @param array $book_link * A fully loaded menu link that is part of the book hierarchy. */ function updateID($book_link) { @@ -387,4 +396,57 @@ function updateID($book_link) { } } + /** + * Builds the parent selection form element for the node form or outline tab. + * + * This function is also called when generating a new set of options during the + * Ajax callback, so an array is returned that can be used to replace an + * existing form element. + * + * @param array $book_link + * A fully loaded menu link that is part of the book hierarchy. + * + * @return + * A parent selection form element. + */ + function addParentSelectFormElements($book_link) { + if ($this->configFactory('menu.settings')->get('override_parent_selector')) { + return array(); + } + // Offer a message or a drop-down to choose a different parent page. + $form = array( + '#type' => 'hidden', + '#value' => -1, + '#prefix' => '
', + '#suffix' => '
', + ); + + if ($book_link['nid'] === $book_link['bid']) { + // This is a book - at the top level. + if ($book_link['original_bid'] === $book_link['bid']) { + $form['#prefix'] .= '' . t('This is the top-level page in this book.') . ''; + } + else { + $form['#prefix'] .= '' . t('This will be the top-level page in this book.') . ''; + } + } + elseif (!$book_link['bid']) { + $form['#prefix'] .= '' . t('No book selected.') . ''; + } + else { + $form = array( + '#type' => 'select', + '#title' => t('Parent item'), + '#default_value' => $book_link['plid'], + '#description' => t('The parent page in the book. The maximum depth for a book and all child pages is !maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)), + '#options' => book_toc($book_link['bid'], $book_link['parent_depth_limit'], array($book_link['mlid'])), + '#attributes' => array('class' => array('book-title-select')), + '#prefix' => '
', + '#suffix' => '
', + ); + } + + return $form; + } + }