diff --git a/core/modules/book/src/BookManager.php b/modules/book/src/BookManager.php index b0cb5976..6243d148 100644 --- a/core/modules/book/src/BookManager.php +++ b/core/modules/book/src/BookManager.php @@ -619,6 +619,13 @@ class BookManager implements BookManagerInterface { $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']; @@ -1103,7 +1110,14 @@ class BookManager implements BookManagerInterface { */ protected function buildBookOutlineRecursive(&$links, $parents, $depth) { $tree = []; + $node = \Drupal::routeMatch()->getParameter('node'); + // The instanceof check doesn't filter out NULLs. + if ($node != NULL && !($node instanceof NodeInterface)) { + $node = $this->entityTypeManager->getStorage('node')->load($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/modules/book/src/Plugin/Block/BookNavigationBlock.php index ed261dca..2df85731 100644 --- a/core/modules/book/src/Plugin/Block/BookNavigationBlock.php +++ b/core/modules/book/src/Plugin/Block/BookNavigationBlock.php @@ -169,6 +169,7 @@ class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInt // 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/modules/book/templates/book-tree.html.twig index 7a818389..f3f203dc 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/themes/classy/templates/navigation/book-tree.html.twig index 94fac559..070e47b6 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', ] %}