Breadcrumbs and path aliasing

Sun, Apr 12, 2009 by Scott

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.

ruben posted on August 21, 2010 10:01 pm

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

Jim posted on April 13, 2009 2:05 pm

Post new comment

The content of this field is kept private and will not be shown publicly.
  • 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> <img> <h2>
  • Lines and paragraphs break automatically.

More information about formatting options