Change Displayed Root of Breadcrumb Trail in Drupal

180 Views Asked by At

How can I change the displayed root of my breadcrumb trail at a certain point?

Lets say my breadcrumbs map the solar system. I want my displayed trail to begin with Solar System until I get to USA, at which point I want USA to be the root. So I would have trails like this for other planets/continents/countries:

Solar System > Jupiter > Big Red Swirl

Solar System > Earth > Australia > Sydney

Solar System > Earth > North America > Canada

But once I get to USA, instead of the breadcrumb trail appearing as...

Solar System > Earth > North America > USA

...I would like USA to become the displayed root

USA

USA > Ohio

USA > California > San Fransisco

Anyone know how I can implement this, whether Drupal module, Drupal technique, or code in my template.php file?

Here is the code in the php template that creates the breadcrumbs:

function cph_main_breadcrumb($variables) {

   $breadcrumb = $variables['breadcrumb'];
   $youAreHere = drupal_get_title();
   $crumbs = ' ';

   if (!empty($breadcrumb)) {
      $crumbs = '<ul>';
      foreach($breadcrumb as $value) {
         $crumbs .= '<li>' .$value.'</li>';
      }
      $crumbs .= '<li>' . $youAreHere . '<li></ul>';
   }
   return $crumbs;
}
1

There are 1 best solutions below

1
On
function MYTHEME_breadcrumb($variables) {

   $breadcrumb = $variables['breadcrumb'];
   //dpm($breadcrumb);
   $youAreHere = drupal_get_title();
   $crumbs = ' ';

   if (!empty($breadcrumb)) {
      $crumbs = '<ul>';
      foreach($breadcrumb as $value) {
        // Add an if statement to check current item
        if($value == '<a href="/MY_PATH_TO_THIS_ITEM">USA</a>') {
          // Reset the list
          $crumbs = '<ul>';
        }
        $crumbs .= '<li>' .$value.'</li>';
      }
      $crumbs .= '<li>' . $youAreHere . '<li></ul>';
   }
   return $crumbs;
}
  • If the current breadcrumb value is the one for "USA" reset the breadcrumb and start it again.

  • If you want to find the current item for USA dpm() the $breadcrumb to find out. Notice that if the link to USA change later you have to change it here also...