« Blank Administration Pages in Drupal 6 after Timeout
» Full Menu Tree with Active Trail in Drupal 6

Drupal, Programming

Making MenuTrails Work with Views 2 in Drupal 6.x

04.21.09 | 6 Comments

Want to make your breadcrumbs work properly when using a Views page and Menu Trails? The following worked for me.

First, the good part: in modules/menutrails/menutrails.module add the following function:

/**
 * Implementation of hook_views_pre_view()
 *
 * This will be invoked on every view request before the view is
 * themed, even if the view is cached
 */
function menutrails_views_pre_view(&$view, &$items) {
  if ($view->display_handler->display->display_plugin=='page') {
    drupal_set_breadcrumb(menutrails_get_breadcrumbs());
  }
}

Now, if you care to read it, the boring explanation.

The way MenuTrails works is by using the so-called Node API hook. This is called (although not directly nor exclusively) by node_load() - the registered “handler” of node content. You see, Drupal has a central registry of methods dealing with accessing and displaying content. One such handler is the node handler, but it is not the only one. Have a look at the menu_router table in the Drupal database for a list. The handler is chosen based on the path of the item.

If you define a view with a page and assign it a path, it gets its very own entry in the menu_router table, pointing to views_page() as the method that handles the display of the view. Node API is completely out of the picture for, I hope, obvious reasons.

The trick then, is to have MenuTrails respond to a different kind of hook - one that is actually invoked somewhere by the Views module. Lucky for us there are several, of which I have somewhat arbitrarily picked hook_views_pre_view(), which, according to the online Drupal documentation, is “called just before a view is themed. This is called even if the query is cached.”

It turns out this works just perfectly for our cause. Right now the snippet of code is as simple as I can make it. If you have any improvements, let me know.

I hope this saves someone a ton of work. It certainly cost me that to find this out, but it was a fruitful experience.

6 Comments

have your say

You can skip to the end and leave a response. Pinging is currently not allowed.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


« Blank Administration Pages in Drupal 6 after Timeout
» Full Menu Tree with Active Trail in Drupal 6