1

I have a situation where I need to mess with a breadcrumb on a page and insert some items in the middle. In my instance drupal_set_breadcrumb() can't be used, so I need to implement a hook_menu_breadcrumb_alter(). I am a little confused about proper usage with this, though, and am not terribly familiar with the guts of how the menu module works.

Is there a proper method for building elements that go into the $active_trail array, eg

$foo = array(
  'title' => $node->title,
  'href' => 'node/' . $node->nid,
  'link_path' => '',
  'localized_options' => array(),
  'type' => 0,
);

title and href are easy enough, but I don't quite get how the other play into this particular context. It is easy enough to figure this out from what is there, but I would like to do "the right thing".

Is there a proper method (or best practice) for testing whether $item is a node and to load it? Again, I can dpm($item), poke around, and get what I need.

I have what I need working, but it is messy and would like to fix it for the long term.

1 Answer 1

1

The properties for each trail item that are documented are the following:

  • title: Title of the item
  • href: Drupal path of the item
  • localized_options: Options for passing into the l() function
  • type: A menu type constant, such as MENU_DEFAULT_LOCAL_TASK, or 0 to indicate it's not really in the menu (used for the home page item)

The code that builds the breadcrumbs is the following.

foreach ($active_trail as $parent) {
  $breadcrumb[] = l($parent['title'], $parent['href'], $parent['localized_options']);
}

The other values (type and link_path) are not used to create the breadcrumbs, but they could be used from other modules that alter the breadcrumbs. Using the default values used from menu_set_active_trail() for those properties probably causes less compatibility problems with other modules.

$trail[] = array(
  'title' => t('Home'), 
  'href' => '<front>', 
  'link_path' => '', 
  'localized_options' => array(), 
  'type' => 0,
);

Is there a proper method (or best practice) for testing whether $item is a node and to load it?

The $item argument hook_menu_breadcrumb_alter() gets is the value returned from menu_get_item(). If you want to check it is for a node, you can use the following code.

if (isset($item['load_functions'][2]) && !empty($item['map'][3]) && $item['load_functions'][4] == 'node_load') {
  $node = $router_item['map'][5];
}
else {
  // The menu item is not for a node.
}

That is the code executed from menu_get_object() when it is called with its default arguments.

References

4
  • Thanks, didn't think to check menu_get_object(). I clarified the portion about the $active_trail.
    – mpdonadio
    Commented Nov 7, 2012 at 19:51
  • I have updated my answer.
    – avpaderno
    Commented Nov 7, 2012 at 22:25
  • You know, I don't know how many times I read that API page and never put two-and-two together about the bottom section. Thanks.
    – mpdonadio
    Commented Nov 7, 2012 at 23:41
  • Writing this answer helped me with finding a bug. I was going crazy rewriting the code all times I added a new link, as $item['map'][1] was interpreted as Markdown link, and rewritten. At the end, I had to manually add Markdown links. :)
    – avpaderno
    Commented Nov 8, 2012 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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