Custom Breadcrumbs in Drupal

Category: 

Aside from the handy dandy Custom Breadcrumbs module and/or using Views arguments to set your breadcrumb, sometimes you just want to set a breadcrumb programmatically on a certain url menu path with phptemplate_breadcrumb, here's how, right now:

function phptemplate_breadcrumb ($breadcrumb) {
  if ($_GET['q'] == 'my_custom/bread/and/butter/url/path') {
   $breadcrumb[] .= l('Bread', 'my-path-to/bread');
   $breadcrumb[] .= l('Butter', 'some-other-path-to/butter');
  }
  if (!empty($breadcrumb)) {
    return '<div class="breadcrumb">' . implode(' › ', $breadcrumb) . '</div>';
  }
}

In the example above, when a user navigates to 'my_custom/bread/and/butter/url/path' the breadcrumb will get 'Bread' and 'Butter' appended to it. If you want to overwrite the breadcrumb instead of append to it, you'll first need to create an empty breadcrumb array. For example, $breadcrumb = array();

This code goes in your theme's template.php file because Ranger McFriendly said so.

Comments

Or you can simply pass an array of links to the drupal_set_breadcrumb() function. That way, you won't have to deal with the markup and the styling.

Here is how ...

global $base_url;
drupal_set_breadcrumb([l("Home", $base_url), l("Link 1", "node/1"), l("Link 2", "node/2"), l("Link 3", "node/3")]);

Cheers

 

Add new comment