how to customize breadcrumbs for page views

petrd - April 18, 2008 - 20:46

Hi folks,
any idea how to customize breadcrumb trails for page views? They show just "Home" by default, which is not what I want.

I'm using the taxonomy_breadcrumb module to generate breadcrumbs for nodes, but this doesn't work for views.

For example, when displaying a node containing an announcement, the breadcrumb is correctly displayed as:
Home > Council > Announcements

but when listing all announcements (using a page view), I get only:
Home
as breadcrumb instead of
Home > Council

Thanks for any help,
Petr

yep, by the time i factor in

arh1 - April 18, 2008 - 21:37

yep, by the time i factor in contributed modules, custom IA and paths, etc, tweaking breadcrumbs seems unavoidable (if you want to use them at all). but i'd hack away at the theme_breadcrumb function (i.e. override it in your theme's template.php), rather than adding another contrib module. this seems like a theming issue to me, and i'd rather not add another dependency if possible. my .02, anyway!

That's what I feared, being

petrd - April 19, 2008 - 07:03

That's what I feared, being not really a coder. Can you maybe advice a bit more how to do this? I'd like to change breadcrumbs only for views. For nodes it's perfectly ok with the taxonomy_breadcrumb module.

Thanks a lot,
Petr

for background, check out

arh1 - April 20, 2008 - 06:44

for background, check out the Drupal 5 Theme Developer's Guide and the page on Using Theme Override Functions.

i'm not sure if it's the best way, or if there's a technique out there that's specific to Views, but what i do is analyze the current path and customize breadcrumbs from that. so in my theme's template.php file, i'll override Drupal's default theme_breadcrumbs function with a function like this:

function themename_breadcrumbs($breadcrumbs) {

  // get the current path, e.g. /council/announcements
  $query = $_REQUEST['q'];

  // it might also be handy to split the path into individual bits:
  $query_bits = explode('/',$query);

  // now add whatever logic you'd like based on the path...

  if ('council/announcements' == $query) {
      array_unshift($breadcrumb, '<a href="/council">Council</a>');
  }

  // finally, return the themed breadcrumbs:
  if (empty($breadcrumb)) {
    return '<div id="breadcrumb">&nbsp;</div>';
  }
  else {
    return '<div id="breadcrumb">'. implode(' &gt; ', $breadcrumb) .'</div>';
  }
}

This worked for me...

ressa - September 12, 2008 - 15:21

Thank you so much arh1, this got me on the right track. I switched the order of things and hand coded the path, to make it 1. work for my situation, and 2. not execute all the time:

// get the current path, e.g. /council/announcements
$query = $_REQUEST['q'];

if ('council/announcements' == $query) {
function themename_breadcrumb($breadcrumbs) {

  // now add whatever logic you'd like based on the path...
  $breadcrumbs = '<a href="' . base_path() . '">Home</a> » <a href="' . base_path() . 'council">Council</a>';

  // finally, return the themed breadcrumbs:
  if (empty($breadcrumbs)) {
    return '<div class="breadcrumb"> &nbsp;</div>';
  }
  else {
    return '<div class="breadcrumb">'. $breadcrumbs .'</div>';
  }
}
}

 
 

Drupal is a registered trademark of Dries Buytaert.