Active trails for menu items in Drupal 7

May 19, 2011

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:

Comments

Permalink

I apply your patch but newly added links to my custom menu called 'menu-club' has no 'active-trail' class.
My variable value is a:6:{i:0;s:10:"navigation";i:1;s:10:"management";i:2;s:9:"user-menu";i:3;s:9:"main-menu";i:4;s:13:"primary-links";i:5;s:9:"menu-club";}.
The original system name for this menu is 'menu-menu-club', then I tried to change last item in object to 'menu-menu-club' but nothing happens.
Any ideas?

Permalink

Hi,
first thank you for this, I stumble on this explanation after searching for almost three days how to make D 7 menu work as I expected with no luck. Here's my problem I use menu block to show my main-menu on a zen sub theme with following structure
-Parent
-Parent
--child
--child
--child
-Parent
and with menu block I could show the whole menu tree but when child is selected I could not make parent to receive "active" state
So please If you can spare some time could you point me how to fix this.
You can see my working copy of the site on http://management.bg/drupal-test

I just want working menu simple as it can be: parents receive active when clicked and show children underneath if they have any and when children is chosen it to receive active and parent to receive active-trail as it's on your site.

Permalink

It's great that you found a bug, fixed it, and contributed a patch, but this might not have been Drupal Planet material.

Permalink

I am testing the context module for drupal 7 and till now I think that this module provides the best solution for menu trails and breadcrumbs.

I am using both of them but with no avail so I am stooping loosing my time with this and will just style menu with class ids its crude and ugly but...
IMHO to build simple menu as that should be more easy in modern CMS

I disagree. It's drupal planet material because it demystifies an issue that can't be rare, saving others frustation and bafflement. I'm glad this was posted, glad to have read it and glad that drupal planet hasn't yet become a dry academic forum exclusive to developers.

What is the value of your site's "menu_default_active_menus" variable? Using drush, type the command drush vget menu_default_active_menus or if you are using phpMyAdmin, look in the variables table and find the variable by that name.

Permalink

Hi there... been having a problem just like this but I can't figure out if the Menu Block would really solve my problems.

I'm trying to make the breadcrumb trail for news pages to show, but can't understand how to put the "article" content type under my hierarchy.

Imagine I just created this "Sample News 2011" article content. I'd like my breadcrumb trail to show

Home > Company News

and the page would seem to be under Home / Company News. Today it only shows

Home >

I just don't want every news to end up inside the menu, right?

any clear thoughts on this?

sorry to bother and thanks in advance.

Permalink

i need to active Menu module plugin active which page i m viewing is it possible with the help of plugin in my custom theme ? ? ? ?

Add new comment

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.