A "book-like" navigation block

Last modified: August 18, 2007 - 01:48

A recent client site, which I was building with Drupal, needed a more conventional navigation system. Specifically, we needed a 'block' to do for regular pages what the book navigation block does for book.module - list the categories in a taxonomy, and show a sublist of that category when you either click on the category name, or access a page which is inside that category.

Here it is. I've used the "collapsed","expanded" and "leaf" list classes to make it all fit neatly into Drupal's default style.

One caveat: It assumes a flat term hierarchy and nodes attached to more than one taxonomy term will be assumed to be a member of only one of them.

Copy and paste this code into a new PHP block, and make a couple of locally-relevant changes (read the comments for details).

<?php

switch (arg(0)) {
  case
'node':
   
$current = arg(1);
   
   
// Note: Change the '7' below to whatever vocabulary ID you're using
   
$cats = taxonomy_node_get_terms_by_vocabulary($current, 7);
    foreach (
$cats as $cat) {
     
$mycategory = $cat->tid;
    }
    break;
  case
'taxonomy':
   
$current = 0;
   
$mycategory = arg(2);
    break;
  default:
   
$current = 0;
   
$mycategory = 0;
    break;
}

// Note: Change the '7' below to whatever vocabulary ID you're using
$terms = taxonomy_get_tree(7);

?>

<div class="menu">
<ul>

<?php

foreach($terms as $term) {
 
$looptid = $term->tid;
 
$loopname = $term->name;
  if(
$looptid == $mycategory) {
    print
'<li class="expanded"><a href="/taxonomy/term/'. $looptid .'">'. $loopname .'</a><ul>';
   
$kiddies = taxonomy_select_nodes(array($mycategory), 'or', 0, FALSE, 'n.title ASC, n.sticky DESC');
    if (
db_num_rows($kiddies) > 0) {
      while (
$getthem = db_fetch_object($kiddies)) {
        print
'<li class="leaf"><a href="/node/"'. $getthem->nid .'">'. $getthem->title .'</a></li>';
      }
    }
    print
'</ul>';
    print
'</li>';
  }
  else {
    print
'<li class="collapsed"><a href="/taxonomy/term/'. $looptid .'">'. $loopname .'</a></li>';
  }
}
?>


</ul>
</div>

You can make it link to path aliases by using the drupal_get_path_alias function:
replace

<?php
print '<li class="expanded"><a href="/taxonomy/term/'. $looptid .'">'. $loopname .'</a><ul>';
?>

with
<?php
print '<li class="expanded"><a href="/'. drupal_get_path_alias('taxonomy/term/$looptid') .'">'. $loopname .'</a><ul>';
?>

and
<?php
print '<li class="collapsed"><a href="/taxonomy/term/'. $looptid .'">'. $loopname .'</a></li>';
?>

with
<?php
print '<li class="collapsed"><a href="/'. drupal_get_path_alias('taxonomy/term/$looptid') .'">'. $loopname .'</a><ul>';
?>

and for the nodes themselves

<?php
print '<li class="leaf"><a href="/node/'. $getthem->nid .'">'. $getthem->title. '</a></li>';
?>

with
<?php
print '<li class="leaf"><a href="/'. drupal_get_path_alias('node/$getthem->nid') .'">'. $getthem->title .'</a></li>';
?>

 
 

Drupal is a registered trademark of Dries Buytaert.