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

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.

<?php
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);
  }
}
?>


Bookmark and Share

4 comments

Anonymous's picture

Nice one.

Last line should be theme('breadcrumb', $breadcrumb)

Janak's picture

Cheers Mosche, code updated :-)

Anonymous's picture

If i want to to make any primary link active, where and how should i do it? im setting menu_set_active_item('mymenuitem'); in template _preprocess_page hook and seems to not work

Janak's picture

@ Marcin:
Can you please post your code so I can see how you are trying to implement this?

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