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.

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

5 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?

Anonymous's picture

very good, thanks, i like

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be 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 can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <apache>, <bash>, <c>, <cpp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>, <vim>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options