Drupal 6: Setting custom breadcrumb and primary navigation to active for any page

Submitted by Janak on Wed, 06/24/2009 - 16:06

Ever wanted to set adhock breadcrumbs for pages that were not really part of any logical menu structure? Also want to activate the primary menu for this ad hoc page?

Drupal 6 makes this easy....

In the rough example below, you can see how I go about setting a custom breadcrumb for taxonomy terms pages without hacking around the drupal core and without installing bunch of additional modules.

UPDATE:
since menu_set_active_item changes the $_GET['q'] value, you may have some unexpected issues if you are using the arg() function after using the menu_set_active_item() function anywhere in your hook_preprocess_page() function.

function mymodule_preprocess_page(&$variables){
  $breadcrumb = mymodule_custom_breadcrumb($variables);
  if($breadcrumb){
    $variables['breadcrumb'] = $breadcrumb;
  }
}
 
 
function mymodule_custom_breadcrumb($variables){
  // taxonomy term page
  if(arg(0) == 'taxonomy' and arg(1) == 'term' and is_numeric(arg(2))){
    $term_obj = taxonomy_get_term(arg(2));
 
    if($term_obj->vid == 1){
      $parents = taxonomy_get_parents($term_obj->tid);
      foreach ($parents as $parent_obj){
        $breadcrumb[] = l($parent_obj->name, taxonomy_term_path($parent_obj));
      }
    }
 
    $breadcrumb[] = l(t('foobar'), 'foobar');
  }
 
  if($breadcrumb){
    $breadcrumb[] = l(t('Home'), NULL);
    $breadcrumb = array_reverse($breadcrumb);
    menu_set_active_item('foobar');
    return theme('breadcrumb', $breadcrumb);
  }
}