Стандартный код из модуля Book, только названия страниц книг показываются развёрнутыми.
<?php
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 - 0, $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 AND n.vid = b.vid 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(0);
?>
Комментарии
Добавлю как комментарий, так как это не самостоятельный сниппет, а только небольшая переделка. Отличия заключаются в том, что строит дерево одной ветки и выводит текст из body родителя. Если нужно вывести в качестве меню, то лучше использовать оригинал. Если ветки не существует, или $book_id == 0, то построит полный список.
<?php
$book_id = '68'; // nid книги, дерево которой нужно построить
$output = '';
$node = node_load(array('nid' => $book_id));
if ($node->nid) {
if ($node->body) {
$output .= $node->body.'<hr />';
}
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 - 0, $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 AND n.vid = b.vid 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>';
}
}
$output .= book_tree_expanded($book_id);
} else {
$output .= book_tree_expanded(0);
}
print $output;
?>