I was surprised that I couldn't find a contrib module that does this out of the box so I created a simple one.
Here it is on Github too:
https://github.com/balsama/bookoblock (bookoblock
= "Book Outline Block")
Info file (bookoblock.info
):
name = Book Outline Block
description = "Provides a block containing an outline of the current book."
package = Other
core = 7.x
dependencies[] = book
Module File (bookoblock.module):
<?php
/**
* Define the Book Outline Block/
* Implementation of hook_block_info()
*/
function bookoblock_block_info() {
$block['book_outline'] = array (
'info' => 'Book Outline',
'cache' => DRUPAL_CACHE_PER_PAGE,
);
return $block;
}
/**
* Define the contents and title of the block/
* Implementation of hook_block_view()
*
* Returns a renderable block array containing the current book's outline if
* the node is part of a book.
*
* Returns NULL if the current menu item is not part of a book heirarchy.
*/
function bookoblock_block_view() {
if ($book = bookoblock_is_book_node()) {
$tree = menu_tree_all_data(book_menu_name($book['bid']));
// If we pass 1 as the second param, we just get the top level node in the
// book. Since the key will be the top level mlid, we'll have to use
// ['p1'] in the $book array (which contains the top mlid) when we use it
// for the block title.
$book_name = (book_toc($book['bid'], 1));
// Build the block title and contents
$block['subject'] = $book_name[$book['p1']];
$block['content'] = menu_tree_output($tree);
// Return the block
return $block;
}
// If the current node isn't part of a book, just return nothing.
return NULL;
}
/**
* Checks to see if current node is part of a book.
* Returns book array if it is part of a book, FALSE is not.
*/
function bookoblock_is_book_node() {
if ((arg(0) == 'node') && (is_numeric(arg(1)))) {
$node = menu_get_object();
if (isset($node->book)) {
return $node->book;
}
}
return FALSE;
}
Just drop those two files into a folder named bookoblock
inside your modules directory and enable the module. You'll now see a new block in the block admin page named Book Outline
. That block will only appear on node pages where the node is part of a book. It contains a heierarchical menu of the entire book and it's title is the root node's title.