15

I'm trying to use this hook in order to alter a breadcrumb link :

hook_menu_breadcrumb_alter(&$active_trail, $item)

The hook don't seem to be called.

I've tried :

  • Using the hook on a theme
  • Using the hook on a custom module (breadcrumb_finder_fix)
  • Clearing all caches using Drush

I have the Devel module installed and activated, and the following doesn't produce any output.

function breadcrumb_finder_fix_menu_breadcrumb_alter(&$active_trail, $item) {
    drupal_set_message(krumo($active_trail));
}

Any ideas ? Directions ? Please advise.

OBS : I've read this answer : https://drupal.stackexchange.com/a/7659/12647

1
  • I'm having the same problem. I've created the MYMODULE_menu_breadcrumb_alter() hook function, enabled the module, cleared the cache, and the hook never seems to be called on user pages. Ironically, it does seem to be called on admin pages (I get the krumo output on admin pages). The theme's _breadcrumb() level seems to be the wrong place to do this. Oct 25, 2013 at 15:44

7 Answers 7

5

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);
      }
    }
    
1
  • @jean I have done few changes, have a look at it(not in the snippet), but in the disclaimer
    – arpitr
    Sep 4, 2013 at 10:25
13

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;
        }       
    }

}
2
  • 2
    This seems to be the right way, but it isn't working for me. Ironically, this does work on admin pages, but it doesn't work on general user content. Suggestions on how to debug? Oct 25, 2013 at 17:59
  • I have the same problem as @PeterMurray. I haven't solved it fully yet but from what I can tell right now, this is caused by the custom_breadcrumbs module somehow overriding the links. My 2 cents, anyway.
    – Pax
    Jan 8, 2014 at 19:20
9

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.

2

this may not be the most efficient way, but try:

function THEME_breadcrumb($variables)

and then read the variables.

... also my god I hate breadcrumbs. so much.

2
  • 1
    I saw this. But it doesn't seems the "Drupal way". This is a theme hook and therefore shouldn't be use to modify the input Apr 9, 2013 at 18:27
  • I'm going for this because the other way isn't working. I'm upvoting your question, but not accepting. Hoping that someone find the "right way". Apr 10, 2013 at 7:33
1

If you only need to alter the breadcrumb on a specific page maybe the following solution can help you. The example updates the breadcrumb for a edit form.

$breadcrumb = array();
$breadcrumb[] = l(t('Home'), '<front>');
$breadcrumb[] = l(t('Nodes listing'), 'nodes/listing');
# Update breadcrumb on the edit form
if (isset($form['nid']) && (is_numeric($form['nid']['#value']))) {
    $breadcrumb[] = l($form['#node']->title, 'node/' . $node->nid);
}

drupal_set_breadcrumb($breadcrumb);

Another Drupal way is to user hook_menu_breadcrumb_alter but make sure to name it YOUR_MODULE_menu_breadcrumb_alter and clear the cache after adding the function.

Or also theme_breadcrumb is a Drupal way to update the breadcrumb but in this hook you should only change the breadcrumb's markup, not links.

1

the THEME_NAME_breadcrumb did not work for me. However, I did manage to override breadcrumb with this function:

function MODULE_node_view_alter(&$build) {
  $node = $build['#node'];
  if ($build['#view_mode'] == 'full' && $node->type == 'CONTENT_TYPE') {
    $breadcrumb = array();
    $breadcrumb[] = l('Home', '<front>');
    $breadcrumb[] = l('People', 'group/node/' . arg(2) . '/people');
    $breadcrumb[] = l('Attendance', 'group/node/' . arg(2) . '/people/attendance');
    drupal_set_breadcrumb($breadcrumb);
  }
}

credit goes to: https://stackoverflow.com/questions/10087168/how-to-add-breadcrumb

0
0
function MODULE_menu_breadcrumb_alter(&$active_trail, $item) {
  // Store the last item
  $end = end($active_trail);
  foreach ($active_trail as $key => $crumb){
    // Check against the node type
    if ($crumb['map'][1]->type == 'CONTENT_TYPE' && $crumb['link_path'] == 'node/%') {
      // Replace the breadcrumb
      $active_trail[$key] = array( 
        'title' => t("Title"),
        'href' => 'PATH',
        'link_path' => 'PATH', 
        'localized_options' => array(),
        'type' => 0
      );
    }
  }  
}

this much code is sufficient to modify breadcrumb url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.