Community Documentation

Integration with Nice Menus module

Last updated April 4, 2009. Created by wchen on April 7, 2007.
Edited by bekasu, add1sun. Log in to edit this page.

this is a variant of the above code, which displays the book contents using nice_menus.module:

<?php
$book_top_page
= 159;
$book_top_title = "menu name";
$levels_deep = 2;
$nice_direction = "right";
$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 class="menuparent">';
         
$struct .= l($node->title, 'node/'. $node->nid);
         
$struct .= $tree;
         
$struct .= '</li>';
        }
        else {
          if (
$children[$node->nid]){
           
$struct .= '<li class="collapsed">'. l($node->title, 'node/'. $node->nid) .'</li>';
          }
          else {
           
$struct .= '<li class="leaf">'. 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, 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'));

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
"<ul class='nice-menu nice-menu-$nice_direction'>".
   
"<li class='menuparent'>".l($book_top_title, 'node/'.$book_top_page).
   
book_struct_recurse($book_top_page, $levels_deep, $children, $current_lineage, $emulate_book_block);
   
"</li></ul>";
?>

This is a working Drupal 6.x version:

<?php
$book_top_page
= 1;
$book_top_title = "try";
$levels_deep = 20;
$nice_direction = "right";
$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 class="menuparent">';
         
$struct .= l($node->title, 'node/'. $node->nid);
         
$struct .= $tree;
         
$struct .= '</li>';
        }
        else {
          if (
$children[$node->nid]){
           
$struct .= '<li class="collapsed">'. l($node->title, 'node/'. $node->nid) .'</li>';
          }
          else {
           
$struct .= '<li class="leaf">'. l($node->title, 'node/'. $node->nid) .'</li>';
          }
        }
      }
     
$struct .= '</ul>';
      return
$struct;
    }
  }
}

if (
$node = menu_get_object('node', 1, 'node/'.$book_top_page)) {
 
// Only display this block when $book_top_page refers to an actual book page
 
$title = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $node->book['bid']));
 
// Only show the block if the user has view access for the top-level node.
 
if ($title) {
   
// first retrieve the book structure from the menu system
   
$result = db_query('SELECT link_path, link_title AS title, mlid, plid FROM {menu_links} WHERE menu_name="%s" ORDER BY weight, link_title', $node->book['menu_name']);
   
$menu = array();
    while (
$m = db_fetch_object($result)) {
     
$menu[$m->mlid] = $m;
     
$menu[$m->mlid]->nid = $nid = (int)substr($m->link_path, 5);
    }
   
// now use the $menu array to build a proper $children and $parent array
   
$children = array();
   
$parent = array();
    foreach (
$menu as $key => $val) {
     
$nid = is_null($menu[$val->plid]->nid) ? 0 : $menu[$val->plid]->nid;
      if (!
$children[$nid]) {
       
$children[$nid] = array();
      }
     
array_push($children[$nid], $val);
     
$parent[is_null($val->nid) ? 0 : (int)$val->nid] = $nid;
    }
   
// $children array properly formed, now get the $current_lineage array
   
$current_lineage = array();
    if (
arg(0) == 'node' && is_numeric(arg(2)) && $parent[arg(2)]) {
     
$nid = arg(2);
      while (
$nid) {
       
array_unshift($current_lineage, $nid);
       
$nid = $parent[$nid];
      }
    }
    echo
"<ul class='nice-menu nice-menu-$nice_direction'>".
       
"<li class='menuparent'>".l($book_top_title, 'node/'.$book_top_page).
       
book_struct_recurse($book_top_page, $levels_deep, $children, $current_lineage, $emulate_book_block);
   
"</li></ul>";
  }
}
?>

Caution

Please review this node: http://drupal.org/node/236418#comment-1024401

For Drupal 6.x, there have been reports that this code does not work.

Comments

Where is

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

Page status

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.