Drupal - Altering the breadcrumb contents

Probably a little late to respond, but I came across this thread this morning whilst looking for a way to override the breadcrumb in Drupal 7, it helped a little so I thought I would add my solution.

This creates extra crumbs based on content types and adds the current page to the crumb. I'm using this to add a crumb to a filtering view seen before the given node.

function MYMODULE_menu_breadcrumb_alter(&$active_trail, $item) {

    //STORE THE LAST ITEM
    $end = end($active_trail);

    foreach ($active_trail as $key => $crumb){

        //CHECK AGAINST NODE TYPE
        if($crumb['map'][1]->type == 'NODE_MACHINE_NAME'){

            //INSERT THE REPLACEMENT CRUMB
            $active_trail[$key] = array( 
                'title' => t("Title"),
                'href' => 'PATH',
                'link_path' => 'PATH', 
                'localized_options' => array(),
                'type' => 0
            );
            //RECREATE ITEM
            $active_trail[] = $crumb;
        }

    }


    //SHOW CURRENT PAGE IN BREADCRUMB BY DUPLICATING THE LAST ARRAY ITEM IN ACTIVE_TRAIL

    if (!drupal_is_front_page()) {
        if ($item['href'] == $end['href']) {
            $active_trail[] = $end;
        }       
    }

}

hook_menu_breadcrumb_alter() does not work on pages where drupal_set_breadcrumb() has been called before.

hook_menu_breadcrumb_alter() is invoked from menu_get_active_breadcrumb(). This is called from drupal_get_breadcrumb(), but only if no breadcrumb has been previously set with drupal_set_breadcrumb().

It does not look like drupal_set_breadcrumb() is called from the user page. But maybe there is a contrib module which does that. You could put a dpm() or ddebug_backtrace() in drupal_set_breadcrumb() to see if this is the case (needs the devel module to be enabled, and you having permission to see devel information).

You can use hook_preprocess_breadcrumb() instead, but this requires to parse the breadcrumb link you want to alter. Because in this hook the breadcrumb links are already rendered as html, so you need regular expressions (preg_match()) or similar to determine e.g. the url.

A much better solution is to use one of the various breadcrumb-customizing modules that are available for Drupal.


Here is a snippet to show how you can alter breadcrumbs.

Keep in mind the following:

  • This method changes breadcrumbs at theme level, which is the last place to change any thing before it reaches to template files, so changing here will not allow other module to change the breadcrumbs later.
  • Consider this way as the last option to alter your breadcrumbs.
  • Add this snippet to template.php file of your theme.

    function THEME_NAME_breadcrumb($variables) {
      $breadcrumb = $variables['breadcrumb'];
    
      // check to ensure this is the one you want to alter
      // Custom rebuild process of breadcrumb with custom links.
      if ($breadcrumb[1] == 'your_breadcrumb_id') {
        // linking to home page
        $links[0] = l(t('Home'), '<front>');
    
        // Keeping the trail/current page as non linked
        $links[1] = ('your desired breadcrumb');
        drupal_set_breadcrumb($links);
      }
    }