Active trails for menu items in Drupal 7

Drupal Association Organization Member Logo

Upgrading to Drupal 7

We've been working on upgrading the DesignHammer website to Drupal 7 for the last few weeks. It's not been a bad process, but one particular issue had been vexing us: how to set the active trail for nodes and pages that don't have menu items. For example, on the DesignHammer website we have a number of Portfolio nodes without menu items. When viewed, the Portfolio link under Work should have the active trail.

Moving from Drupal 6 to Drupal 7

In Drupal 6, this was accomplished easily with the popular Menu Trails module. Problem is, Menu Trails doesn't have a Drupal 7 port. And to make things more complicated, there is a nasty Drupal core bug related to the active trail issue.

After some searching, I came across "Use menus in Drupal 7? You need Menu block module." But despite using Menu Block and sMenu Position, we still weren't seeing the desired results.

After looking through the code in Menu Block, I found out why. Menu Block has a function menu_block_fix_custom_menus() that handles setting the Drupal core variable menu_default_active_menus with information about the active menus on the site. Here's a snippet of the function:

If you are working on a new site build in Drupal 7, you won't have any problems with this. But if, like us, you are upgrading from Drupal 6 to Drupal 7, then your main navigation menus are likely called "primary-links" and "secondary-links". As a result, the second check in the if statement is going to return false, and then your primary links menu won't get added to the active menus variable.

I submitted a patch to Menu Block for this issue. You can also use the Execute PHP block provided by Devel, build the menu_default_active_menus array yourself, then set it with variable_set().

For example, you could do something like:

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.