diff --git a/core/modules/book/src/BookManager.php b/core/modules/book/src/BookManager.php index bfe55881af..dfa0f90407 100644 --- a/core/modules/book/src/BookManager.php +++ b/core/modules/book/src/BookManager.php @@ -591,6 +591,13 @@ protected function buildItems(array $tree) { $element['in_active_trail'] = TRUE; } + // Set a helper variable to indicate whether the link belongs to + // the current page. + $element['is_active'] = FALSE; + if ($data['link']['is_active']) { + $element['is_active'] = TRUE; + } + // Allow book-specific theme overrides. $element['attributes'] = new Attribute(); $element['title'] = $data['link']['title']; @@ -1080,7 +1087,10 @@ protected function buildBookOutlineData(array $links, array $parents = [], $dept */ protected function buildBookOutlineRecursive(&$links, $parents, $depth) { $tree = []; + $node = \Drupal::routeMatch()->getParameter('node'); while ($item = array_pop($links)) { + // Check if item belongs to the current page. + $item['is_active'] = ($node && $node->id() == $item['nid']) ? TRUE : FALSE; // We need to determine if we're on the path to root so we can later build // the correct active trail. $item['in_active_trail'] = in_array($item['nid'], $parents); diff --git a/core/modules/book/src/Plugin/Block/BookNavigationBlock.php b/core/modules/book/src/Plugin/Block/BookNavigationBlock.php index e55fe36525..e784d713a1 100644 --- a/core/modules/book/src/Plugin/Block/BookNavigationBlock.php +++ b/core/modules/book/src/Plugin/Block/BookNavigationBlock.php @@ -140,6 +140,7 @@ public function build() { // Since we know we will only display a link to the top node, there // is no reason to run an additional menu tree query for each book. $book['in_active_trail'] = FALSE; + $book['is_active'] = FALSE; // Check whether user can access the book link. $book_node = $this->nodeStorage->load($book['nid']); $book['access'] = $book_node->access('view'); diff --git a/core/modules/book/templates/book-tree.html.twig b/core/modules/book/templates/book-tree.html.twig index bf7424f4ef..6e8196e972 100644 --- a/core/modules/book/templates/book-tree.html.twig +++ b/core/modules/book/templates/book-tree.html.twig @@ -16,6 +16,7 @@ * - is_collapsed: TRUE if the link has children within the current book tree * that are not currently visible. * - in_active_trail: TRUE if the link is in the active trail. + * - is_active: TRUE if the link is for the current page. * * @ingroup themeable */ diff --git a/core/themes/classy/templates/navigation/book-tree.html.twig b/core/themes/classy/templates/navigation/book-tree.html.twig index 186a547e14..68043be52f 100644 --- a/core/themes/classy/templates/navigation/book-tree.html.twig +++ b/core/themes/classy/templates/navigation/book-tree.html.twig @@ -16,6 +16,7 @@ * - is_collapsed: TRUE if the link has children within the current book tree * that are not currently visible. * - in_active_trail: TRUE if the link is in the active trail. + * - is_active: TRUE if the link is for the current page. */ #} {% import _self as book_tree %} @@ -41,6 +42,7 @@ item.is_expanded ? 'menu-item--expanded', item.is_collapsed ? 'menu-item--collapsed', item.in_active_trail ? 'menu-item--active-trail', + item.is_active ? 'menu-item--active', ] %}