Drupal Tip: Menu Trails By Path Module

  —   12 October 2014   —   Code & Drupal

Hansel and Gretel Breadcrumbs

A simple module to ease a common Drupal pain point - active menu items and breadcrumb trails

I’m a big believer in website breadcrumb trails. They help to give context to a piece of content by showing where it sits in the website structure – especially useful if the user landed on this page from an external link. An accurate breadcrumb trail also allows the user to reverse direction if they need to back up through the content. In partnership with the breadcrumb, the user also expects the current page (and any ancestors) to be visually marked as active in the menu.

The default Drupal active trail functions can only accurately set the breadcrumb when every piece of content has its own link in a menu. Normally this would be a hierarchical menu tree in the default Main Menu (or Primary Menu). Very soon, though, your project will divert from the menu-item-for-every-page method, probably when you start creating listings with the Views module.

Consider the Blog section of a simple website. The Blog listing page is almost certainly going to be a View of a Blog Post content type. You wouldn’t expect to add the posts to the menu itself because the menu would soon become bloated. Without a menu item, though, Drupal can’t infer which section is active and can’t build an accurate breadcrumb trail. These ‘orphaned’ pages cause pain for the user who has to navigate back to the Blog section to continue reading.

Consider also how this could become much more difficult when you factor in additional content types such as latest news, product listing, FAQ list, staff directory, job vacancies…

The Tip - Menu Trail By Path

We want to convince Drupal that the Blog menu item is active when we’re reading any Blog Post content. In the past I’ve used various modules including Menu Position and Context to setup individual rules per content type, but save yourself some time and go straight for Menu Trail By Path module.

Menu Trail By Path module examines the current URL and sets the active-trail on menu items that appear to be ancestors. The forward slash is assumed to divide ancestors levels.

Say we’ve written a new blog article with this URL:
/blog/my-category/my-interesting-article

Menu items that point to any of these URLs will be set active:-
/blog/my-category/my-interesting-article
/blog/my-category
/blog  <-- that's the one!

Once Drupal has an active menu trail then the breadcrumb trail comes for free:-
Home > Blog > My Category > My Interesting Article

No specific configuration is required, but you will have to be following best practice URL patterns. You must set sensible Pathauto patterns for your content types that include the hierarchy of their ancestor pages for this to work. You might want to leverage Token and Entity Tokens to help you create meaningful URLs. 



Our Partners / Accreditations / Networks

0161 872 3455

Sign up to our newsletter