Drupal 7: Render book menu

Since Drupal 6 things have got a little trickier in terms of rendering your own book menu, after quite a bit of hunting through the books.module codebase and Drupals API site, I came up with this snippet to generate a book index for any book. Just supply the node id for the book you wish to render.

// Print out the HTML for the book that node 17 belongs to
print anthill_render_book_nav(17);
// Render HTML output for a given book
function anthill_render_book_nav($nid=NULL){
	if(!$nid) return;
	$node = node_load($nid);
	if(isset($node->book)) $mlid = $node->book['mlid'];
	else return;
	$menuname = $node->book['menu_name'];
	$tree = menu_build_tree($menuname);
	$treeoutput = menu_tree_output($tree);
	return $output = drupal_render($treeoutput);


Menu block module is pretty

Menu block module is pretty useful in some instances:


if you set it a block to use 'current menu' and then disable all but Book navigation at: /admin/config/user-interface/menu-block

then you get a book menu from specified depth etc

Dude, life is saved. Thanks.

Dude, life is saved. Thanks.

You can put it pretty much

You can put it pretty much anywhere - in a block, in a theme template etc.

Where does that go?

Where does that go?

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h2> <h4> <img> <div> <table> <tr> <td> <pre>
  • Lines and paragraphs break automatically.
  • Insert image: [img:node_id align=(left|right) size=(|fullLA|fullPreview|fullSQ|halfLA|halfPO|halfSQ|thirdSQ) action=(popup|link|frame) url=url style="margin:10px;" title=text description=text]

More information about formatting options

info@theanthillsocial.co.uk XHTML 1.0 Strict CSS2 WAI