Using Rules to set active menu items

Setting active menu items in Drupal can be quite a hazzle, and there are a number of different modules trying to solve the problem. This screencast shows how Menu position can be used together with Rules and Rules Bonus Pack to set active menu items in a quick and flexible way – reusing existing frameworks for Drupal.

Unlike some content management systems, Drupal doesn't force you to put content at a particular place in a menu structure. You may choose to do so manually – but often times you would like menu items not to be individual pieces of content, but rather lists or other overview pages. On a news site, for example, you'd probably prefer to have a menu item sports listing all sports news – but not all sports news items being a part of the menu structure.

This is all good and well, but it also creates a problem known to many in the Drupalverse – the menu often loses track of which item should be the active one. In the sports example above, you'd probably want the sports menu item to be active when reading a sports news item – but Drupal doesn't have a natural way of knowing which menu item should be active. If the viewed page isn't in the menu structure, no menu item is active. Period.

There are a number of modules attempting to solve this problem, such as Context, Taxonomy Menu Trails, Menu Trail By Path and more. This screencast shows how the Menu position module can be used together with Rules and Rules Bonus Pack to set active menu items in a quick and flexible way.

Why this combination? Well:

  • Menu position changes the actual active menu item (or actually the cache representing it) – it doesn't just apply a CSS class. This means that you can view sub items in the menu.
  • Rules provides a framework for triggering actions on selected events, and it makes sense to reuse the powerful and flexible framework of Rules. It means, for example, that you (1) can leverage the conditions available in Rules to select exactly under which circumstances you want to activate a menu item, and (2) that if you need some additional events or conditions, you can write these as Rules plugins – available for other mechanics on your site as well.

Do you think this makes sense? Do you think it is crazy? Do you have questions, ideas, suggestions, or a haiku that you want to get off your chest? Post a comment!

Note: Cred to pontus_nilsson and geekyMoa for great contributions to the Rules action demonstrated in this screencast!