Breadcrumbs and path aliasing
Posted on: Sunday, April 12th 2009 by Scott Bell

I came across a problem with Drupal's breadcrumbs not working properly with path aliasing. For instance, you're in the 'about' section...then you go to a sub-page of that section and the url says 'about/people'. What this friendly url actually means though is node/8. So if I'm creating page nodes and aliasing them to 'about/contact' and 'about/services', it's deceiving because these paths really don't have any sort of relationship to one another, other than a visual one created by the path aliasing. Drupal's breadcrumb isn't smart enough to interpret all these paths as being related, inside the 'about' section so I had to make some modifications.

In template.php:

/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return a string containing the breadcrumb output.
 */
function phptemplate_breadcrumb($breadcrumb) { 
  if (!empty($breadcrumb)) {
      
      $path = explode('/', $_SERVER['REQUEST_URI']);
      $title = drupal_get_title();   
 	if($path[2]) {
 	  $breadcrumb[] = l(ucfirst($path[1]), $path[1]);
 	  $breadcrumb[] = $title; 
        }
   return implode(' › ', $breadcrumb);
  }
}

Basically what this is doing is creating a breadcrumb trail based on the actual url that you are seeing in the browser...grabbed via the $_SERVER['REQUEST_URI'] variable. Then it separates the url path at the '/'.

This solved my problem. Hopefully it solves someone elses as well! I'd be interested in seeing other examples of how to use breadcrumbs with path aliasing.

Comments

man, you saved me hours of headache, many thanks, works perfectly.

Thanks a lot .. you saved hours of googling .. it works like a charm!

Post new comment

The content of this field is kept private and will not be shown publicly.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.