summaryrefslogtreecommitdiffstats
path: root/core/modules/book/src/BookManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules/book/src/BookManager.php')
-rw-r--r--core/modules/book/src/BookManager.php75
1 files changed, 48 insertions, 27 deletions
diff --git a/core/modules/book/src/BookManager.php b/core/modules/book/src/BookManager.php
index aab9611..711976a 100644
--- a/core/modules/book/src/BookManager.php
+++ b/core/modules/book/src/BookManager.php
@@ -16,6 +16,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\Template\Attribute;
use Drupal\node\NodeInterface;
/**
@@ -501,20 +502,46 @@ class BookManager implements BookManagerInterface {
* {@inheritdoc}
*/
public function bookTreeOutput(array $tree) {
- $build = array();
- $items = array();
+ $items = $this->buildItems($tree);
- // Pull out just the book links we are going to render so that we
- // get an accurate count for the first/last classes.
- foreach ($tree as $data) {
- if ($data['link']['access']) {
- $items[] = $data;
- }
+ $build = [];
+
+ if ($items) {
+ // Make sure drupal_render() does not re-order the links.
+ $build['#sorted'] = TRUE;
+ // Get the book id from the last link.
+ $item = end($items);
+ // Add the theme wrapper for outer markup.
+ // Allow menu-specific theme overrides.
+ $build['#theme'] = 'book_tree__book_toc_' . $item['original_link']['bid'];
+ $build['#items'] = $items;
+ // Set cache tag.
+ $build['#cache']['tags'][] = 'config:system.book.' . $item['original_link']['bid'];
}
- $num_items = count($items);
- foreach ($items as $i => $data) {
+ return $build;
+ }
+
+ /**
+ * Builds the #items property for a book tree's renderable array.
+ *
+ * Helper function for ::bookTreeOutput().
+ *
+ * @param array $tree
+ * A data structure representing the tree.
+ *
+ * @return array
+ * The value to use for the #items property of a renderable menu.
+ */
+ protected function buildItems(array $tree) {
+ $items = [];
+
+ foreach ($tree as $data) {
$class = ['menu-item'];
+ // Generally we only deal with visible links, but just in case.
+ if (!$data['link']['access']) {
+ continue;
+ }
// Set a class for the <li>-tag. Since $data['below'] may contain local
// tasks, only set 'expanded' class if the link also has children within
// the current book.
@@ -528,30 +555,24 @@ class BookManager implements BookManagerInterface {
// Set a class if the link is in the active trail.
if ($data['link']['in_active_trail']) {
$class[] = 'menu-item--active-trail';
- $data['link']['localized_options']['attributes']['class'][] = 'menu-item--active-trail';
}
// Allow book-specific theme overrides.
- $element['#theme'] = 'book_link__book_toc_' . $data['link']['bid'];
- $element['#attributes']['class'] = $class;
- $element['#title'] = $data['link']['title'];
+ $element = [];
+ $element['attributes'] = new Attribute();
+ $element['attributes']['class'] = $class;
+ $element['title'] = $data['link']['title'];
$node = $this->entityManager->getStorage('node')->load($data['link']['nid']);
- $element['#url'] = $node->urlInfo();
- $element['#localized_options'] = !empty($data['link']['localized_options']) ? $data['link']['localized_options'] : array();
- $element['#below'] = $data['below'] ? $this->bookTreeOutput($data['below']) : $data['below'];
- $element['#original_link'] = $data['link'];
+ $element['url'] = $node->urlInfo();
+ $element['localized_options'] = !empty($data['link']['localized_options']) ? $data['link']['localized_options'] : [];
+ $element['localized_options']['set_active_class'] = TRUE;
+ $element['below'] = $data['below'] ? $this->buildItems($data['below']) : [];
+ $element['original_link'] = $data['link'];
// Index using the link's unique nid.
- $build[$data['link']['nid']] = $element;
- }
- if ($build) {
- // Make sure drupal_render() does not re-order the links.
- $build['#sorted'] = TRUE;
- // Add the theme wrapper for outer markup.
- // Allow book-specific theme overrides.
- $build['#theme_wrappers'][] = 'book_tree__book_toc_' . $data['link']['bid'];
+ $items[$data['link']['nid']] = $element;
}
- return $build;
+ return $items;
}
/**