Adding Custom Breadcrumb for Drupal nodes

Submitted by Janak on Sun, 11/01/2009 - 11:53

Out of the box Drupal does not add breadcrumb to node pages, in most situations this is not a problem until recently, I had decided to install the excellent Drupal Forums module on one of the work websites. It quickly became apparent that a forum without a logical breadcrumb is not as user friendly, so here is what I wanted in the breadcrumb:

- Link to Home (Only item Drupal adds by default)
- Link to content type view, eg, Forums Home page, Blog Home page etc
- Links to nested taxonomy terms the node is tagged with

Here is a simple implementation of hook_nodeapi that you can use your in your own modules..

function mymodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'view':
        if($page) {
          $breadcrumb = array();
          // lets add node taxonomy as a breadcrumb
          foreach ($node->taxonomy as $term_obj){         
            //add the current term
            $breadcrumb[] = l($term_obj->name, taxonomy_term_path($term_obj));
            //lets sort out the parents for current term 
            $parents = taxonomy_get_parents($term_obj->tid);
            foreach ($parents as $parent_obj){
              $breadcrumb[] = l($parent_obj->name, taxonomy_term_path($parent_obj));
          //if you already have a view for each of your content types
          //you can add a breadcrumb item to the content type view            
          if($node->type =="forum"){
            $breadcrumb[] = l(t('Forum'), 'forum');
          elseif($node->type =="blog"){
            $breadcrumb[] = l(t('Blog'), 'blog');
          //elseif($node->type =="my_cck_type"){
          //  $breadcrumb[] = l(t('my_cck_type_name'), 'my_cck_type_view_path');
          $breadcrumb[] = l(t('Home'), NULL); 
          $breadcrumb = array_reverse($breadcrumb);