My question is about how to show the entire book navigation tree in a block (drupal 8)

Thanks to @oknate we can have a Book Navigation block with the top level book showing, But i need the whole tree. (think table of contents)

All the folowing examples assume you are are the top page of a 3 level deep book.

Also, I have a working module at https://github.com/petergus/alt-book-nav.git

Showing the title (top level) book in the book navigation block is done by replacing the following in book/src/Plugin/Block/BookNavigationBlock.php (with a custom module of course)

 // Only show the block if the user has view access for the top-level node.
  if ($nid) {
    $tree = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
    // There should only be one element at the top level.
    $data = array_shift($tree);
    $below = $this->bookManager->bookTreeOutput($data['below']);
    if (!empty($below)) {
      return $below;


     if ($nid) {
        $tree = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
        return $this->bookManager->bookTreeOutput($tree);

That only shows the top level and child level though, I want to be able to view the full tree from any page.

I am completely new with drupal 8 and not very handy with PHP so all i could find out was that

     $tree = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);

returns only the book name and first level and

     $tree = $this->bookManager->bookTreeAllData($node->book['bid']);

(without the $node->book) returns multiples levels, that is, multiple 'below' arrays.

enter image description here

I have no idea how to go further with pulling that information into the block, does someone?

As mentioned above, I have the module that @oknate suggested at https://github.com/petergus/alt-book-nav.git

Greatly appreciating any help, and hoping this questions serves the community.


It turns that I already posted the answer in the question, but somehow missed it during testing!

Removing , $node->book from $tree = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book); does print the entire expanded tree.

If someone has time to add a user form option to the module it could eventually benefit many and get on the drupal.org repo.

Not the answer you're looking for? Browse other questions tagged or ask your own question.