Drupal - How to set a menu item active ?

Their is a lot of ways. I list here only things compatible with D7 and eventually on D6.

Use a modules

  • https://drupal.org/project/menu_position (need rules, watch http://vimeo.com/27074705)
  • https://drupal.org/project/menu_trail_by_path
  • https://drupal.org/project/context

Code your stuff

Their is now a native API menu_tree_set_path to set the path you want to use to calculate the menu active trail.

You can also work in theme_links but but I recommend to use menu_tree_set_path so other module know what is your active link. The advantage is you can use this function in your theme without creating a module.

Keep in mind, menu_position has an API to create your custom rules.

So what should I use???

It's depend of your need. if you have simple rules like: for this node type/views/taxonomy i want this active menu, context is good.

If you have a good url structure (autopath) than follow your menu structure, menu_trail_by_path will just works without any effort.

I never used menu_position... so I can very tell.

If you need some special logic, coding your things is certainly faster than creating 40 context or rules...


Context provides this functionality out of the box.


The easiest way is just to make those menu items of your "internal" pages (subpages belonging to your areas/landing pages) children of landing pages menu items.

Something like:

primary links menu
|
+-- landing page 1 menu item
|   |
|   +-- internal page 1 menu item
|   +-- internal page 2 menu item
|
+-- landing page 2 menu item
    |
    +-- internal page 3 menu item
    +-- internal page 4 menu item

If you have to keep them in separate menus though, you would probably need to play with theme_links() to keep proper "active-trail" and/or "active" classes.

Tags:

Routes

7