Just copy the code into a php custom block. Make sure you replace $book_top_page with the book id you wish to show.

Drupal 6.x

(This was added by user littlelloyd for Drupal 6.)

<div class="whole-book-menu">
<?php
$book_top_page = 1;
$levels_deep = 3;
$emulate_book_block = true;

if (!function_exists('book_struct_recurse')){
function book_struct_recurse($nid, $levels_deep, $children, $current_lineage = array(), $emulate_book_block = true) {
$struct = '';
if ($children[$nid] && ($levels_deep > 0 || ($emulate_book_block && in_array($nid, $current_lineage)))) {
$struct = '<ul>';
      foreach ($children[$nid] as $key => $node) {
      if ($tree = book_struct_recurse($node->nid, $levels_deep - 1, $children, $current_lineage, $emulate_book_block)) {
      $struct .= '<li>';
      $struct .= l($node->title, 'node/'. $node->nid);
      $struct .= $tree;
      $struct .= '</li>';
      }
      else {
      if ($children[$node->nid]){
      $struct .= '<li>'. l($node->title, 'node/'. $node->nid) .'</li>';
      }
      else {
      $struct .= '<li>'. l($node->title, 'node/'. $node->nid) .'</li>';
      }
      }
      }
      $struct .= '</ul>';
return $struct;
}
}
}

$current_lineage = array();

$result = db_query(db_rewrite_sql('SELECT n.nid, n.title, n2.nid parent, ml.weight
FROM {node} n
INNER JOIN {book} b ON n.nid = b.nid
INNER JOIN {menu_links} ml ON b.mlid = ml.mlid
INNER JOIN {book} b2 on b2.mlid = ml.plid
INNER JOIN {node} n2 on b2.nid = n2.nid
WHERE n.status =1
ORDER BY ml.weight, n.title'));

while ($node = db_fetch_object($result)) {
if (!$children[$node->parent]) {
$children[$node->parent] = array();
}
array_push($children[$node->parent], $node);

/*  This function is broken, and for my purposes, not needed **********
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(1) == $node->nid) {
  $_temp = book_location($node);
  foreach ($_temp as $key => $val){
    $current_lineage[] = $val->nid;
  }
  $current_lineage[] = arg(1);
}
*/
}

echo book_struct_recurse($book_top_page, $levels_deep, $children, $current_lineage, $emulate_book_block);

?></div>

Drupal 5.x

<div class="whole-book-menu">

<?php
$book_top_page = 63;
$levels_deep = 2;
$emulate_book_block = true;

if (!function_exists('book_struct_recurse')){
function book_struct_recurse($nid, $levels_deep, $children, $current_lineage = array(), $emulate_book_block = true) {
$struct = '';
if ($children[$nid] && ($levels_deep > 0 || ($emulate_book_block && in_array($nid, $current_lineage)))) {
$struct = '<ul>';
      foreach ($children[$nid] as $key => $node) {
      if ($tree = book_struct_recurse($node->nid, $levels_deep - 1, $children, $current_lineage, $emulate_book_block)) {
      $struct .= '<li>';
      $struct .= l($node->title, 'node/'. $node->nid);
      $struct .= $tree;
      $struct .= '</li>';
      }
      else {
      if ($children[$node->nid]){
      $struct .= '<li>'. l($node->title, 'node/'. $node->nid) .'</li>';
      }
      else {
      $struct .= '<li>'. l($node->title, 'node/'. $node->nid) .'</li>';
      }
      }
      }
      $struct .= '</ul>';
return $struct;
}
}
}

$current_lineage = array();

// use this version for Drupal 4.6
// $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 ORDER BY b.weight, n.title'));

// use this version for Drupal 4.7 and Drupal 5
$result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.nid = b.nid AND n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'));

while ($node = db_fetch_object($result)) {
if (!$children[$node->parent]) {
$children[$node->parent] = array();
}
array_push($children[$node->parent], $node);

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(1) == $node->nid) {
$_temp = book_location($node);
foreach ($_temp as $key => $val){
$current_lineage[] = $val->nid;
}
$current_lineage[] = arg(1);
}
}

echo book_struct_recurse($book_top_page, $levels_deep, $children, $current_lineage, $emulate_book_block);

?></div>

Comments

kenorb’s picture

Where is book_struct_recurse() defined? I can't find it in 6.15

kenorb’s picture

mErilainen’s picture

For D6, this snippet doesn't open the child pages in the menu which the book might have.

mErilainen’s picture

For D6, this snippet doesn't open the child pages in the menu which the book might have.

sreyas’s picture

Used this code to get the block appear only on the book pages.

<?php
$bresval = false;

if (arg(0) == 'node' && ctype_digit(arg(1))) {
  $node = node_load(arg(1));
  if ($node->type == 'book') {
    $bresval = true;
  }   
}
return $bresval;
?>

------------------
Ciril Sreedhar
Sreyas IT Solutions
Server Administration | Web Designing | Web Programming

functions’s picture

If the current node page is part of a book, this code displays a block with links to other pages in that book. I added a tiny bit of code to the awesome snippet above to make the book menu also appear on the top-level page of the book. (Drupal 6)

<?php

if ( arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {
  $node = node_load(arg(1));
 

if(isset($node->book)) { // book page
      if($parent = book_link_load($node->book['plid']) ) { // if current page is child book page
      $pid = $parent['nid'];
      } else { $pid = $node->nid; } // if current page is top-level book page
?>

<div class="whole-book-menu">
<?php
$book_top_page = $pid;
$levels_deep = 3;
$emulate_book_block = true;

if (!function_exists('book_struct_recurse')){
function book_struct_recurse($nid, $levels_deep, $children, $current_lineage = array(), $emulate_book_block = true) {
$struct = '';
if ($children[$nid] && ($levels_deep > 0 || ($emulate_book_block && in_array($nid, $current_lineage)))) {
$struct = '<ul>';
      foreach ($children[$nid] as $key => $node) {
      if ($tree = book_struct_recurse($node->nid, $levels_deep - 1, $children, $current_lineage, $emulate_book_block)) {
      $struct .= '<li>';
      $struct .= l($node->title, 'node/'. $node->nid);
      $struct .= $tree;
      $struct .= '</li>';
      }
      else {
      if ($children[$node->nid]){
      $struct .= '<li>'. l($node->title, 'node/'. $node->nid) .'</li>';
      }
      else {
      $struct .= '<li>'. l($node->title, 'node/'. $node->nid) .'</li>';
      }
      }
      }
      $struct .= '</ul>';
return $struct;
}
}
}

$current_lineage = array();

$result = db_query(db_rewrite_sql('SELECT n.nid, n.title, n2.nid parent, ml.weight
FROM {node} n
INNER JOIN {book} b ON n.nid = b.nid
INNER JOIN {menu_links} ml ON b.mlid = ml.mlid
INNER JOIN {book} b2 on b2.mlid = ml.plid
INNER JOIN {node} n2 on b2.nid = n2.nid
WHERE n.status =1
ORDER BY ml.weight, n.title'));

while ($node = db_fetch_object($result)) {
if (!$children[$node->parent]) {
$children[$node->parent] = array();
}
array_push($children[$node->parent], $node);

/*  This function is broken, and for my purposes, not needed **********
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(1) == $node->nid) {
  $_temp = book_location($node);
  foreach ($_temp as $key => $val){
    $current_lineage[] = $val->nid;
  }
  $current_lineage[] = arg(1);
}
*/
}

echo book_struct_recurse($book_top_page, $levels_deep, $children, $current_lineage, $emulate_book_block);

?></div>
<?php
}


}
 ?>