Adding Custom Breadcrumb for Drupal nodes

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..

<?php
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);
         
drupal_set_breadcrumb($breadcrumb);        

        }

      break;
  }
}
?>


Bookmark and Share

5 comments

Anonymous's picture

Why not just use the custom breadcrumbs module?

Anonymous's picture

If you are not a coder make sure and check out the Custom Breadcrumb module the 2.x version rocks!

Anonymous's picture

Not often I see "excellent" before "Drupal forum". LOL!

Just wanted to point out that forum nodes do, actually, have breadcrumbs. See http://shellsn.com/node-611-forum for an example.

Michelle

Janak's picture

Thanks for the comments and suggestions for Custom Breadcrumb module. I have to agree that it does the job really well (if you are not a coder). I tried it on this website but I wanted more control over the breadcrumbs on node and non-node pages, so I had to look into implementing my custom code, which is only there to serve as a basis for your custom requirements. The possibilities are endless..

@michelle: Yes you are right, on its own Drupal forums are very basic, but combine it with Advanced Forum Advanced Profile among other modules and it really isnt too bad, besides you can still harness the Drupal goodness.

Its strange that my install of Drupal Forums didnt come with Breadcrumb out of the box. Also one quick observation on the site you posted above, the root path to the forums is "forum" however the taxonomy is configured with "forums/foobar", I was almost in the same boat until I configured pathauto with "forum/[catpath]" to keep url structure consistent.

Anonymous's picture

Oh, I know that you can do a lot with Drupal's forums. It's just rare for me to hear other people saying nice things about it. LOL

As for the forum/forums thing, yeah, that's a pain for hackable URLs. I had an issue in AF's queue about it but I don't think it's a good idea for AF to change the URL from how core sets it. I'm probably going to set up a redirect for that on my site. That seems to be the best path.

Michelle

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options