Hi,

I have not been able to find this anywhere.

I want to creata a table of contents for a book by expanding all children recursively.

Comments

Thomas Leonard’s picture

This seems to work (change the 1 on the last line to the book number).

function book_tree_recurse_all($nid, $depth, $children) {
  if ($depth > 0) {
    if ($children[$nid]) {
      foreach ($children[$nid] as $foo => $node) {

          if ($tree = book_tree_recurse_all($node->nid, $depth - 1, $children)) {
            $output .= '<li class="expanded">';
            $output .= l($node->title, 'node/'. $node->nid);
            $output .= '<ul>'. $tree .'</ul>';
            $output .= '</li>';
          }
          else {
            $output .= '<li class="leaf">'. l($node->title, 'node/'. $node->nid) .'</li>';
          }
      }
    }
  }

  return $output;
} 
function book_tree_expanded($parent = 0, $depth = 3) {
  $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 AND n.moderate = 0 ORDER BY b.weight, n.title'));

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

  if ($tree = book_tree_recurse_all($parent, $depth, $children)) {
    return '<div class="menu"><ul>'. $tree .'</ul></div>';
  }
}
print book_tree_expanded(1);

I just copied the book functions which print a non-expanded tree and removed the unfold argument.

Thomas Leonard’s picture

Apply this patch to modules/book.module (Drupal 4.6):

--- book.old    2006-01-05 09:39:31.000000000 +0000
+++ book.new    2006-01-05 09:43:38.000000000 +0000
@@ -459,7 +459,7 @@
   if ($node->nid) {
     $output .= '<div class="book">';co
 
-    if ($tree = book_tree($node->nid)) {
+    if ($tree = book_tree($node->nid, 3, '*')) {
       $output .= '<div class="tree">'. $tree .'</div>';
     }
 
@@ -539,7 +539,7 @@
   if ($depth > 0) {
     if ($children[$nid]) {
       foreach ($children[$nid] as $foo => $node) {
-        if (in_array($node->nid, $unfold)) {
+        if ($unfold == '*' || in_array($node->nid, $unfold)) {
           if ($tree = book_tree_recurse($node->nid, $depth - 1, $children, $unfold)) {
             $output .= '<li class="expanded">';
             $output .= l($node->title, 'node/'. $node->nid);

This changes the display of the list of children at the bottom of each book page to include sub-sections.

mboy-1’s picture

Thanks!

mboy-1’s picture

Hi!

I like this, but how to hide all revisions use this program? Thanks!

Daniel

Horla’s picture

Change $result from

$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 AND n.moderate = 0 ORDER BY b.weight, n.title'));

to

$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 AND n.moderate = 0 ORDER BY b.weight, n.title'));

This works on my site (4.7.3). A little late but I ran today into this problem and when I search on TOC and book this is the first topic on the list so an update could be handy.

urbanfalcon’s picture

If you don't want to hack into your module, try going through my homepage to get a copy of "book plus" - it has admin settings for tables of contents, including an interactivity option (to expand/contract tables of contents via javascript).

whereisian’s picture

I'll be checking out Book Plus first chance I get. Have you considered posting this on drupal.org? It seems like a most worthy addition.

urbanfalcon’s picture

Book Plus replaces book.module completely - this is done by design so that you can transform the view and edit screen presentations instantly without having to change their content types or make database alterations. It's a non-destructive, 1:1 switch. However, I believe that offering one module to replace another would not be well received. I've been asked to make a patch that can be applied to the original book module, but there are SO many changes now - the patch would be to delete everything and insert the new code! Rather than extinguish more flames about how I shouldn't offer altered core modules as add-ons, how book is dead because it's an inferior content type, etc., so forth, I've just opted to maintain it independently for now.