LevelTen Interactive

Add menu sub-navigation as a sidebar block in Drupal

Follow Me On Twitter

I am currently on a project that requires a 2-level drop down navigation and a sidebar block that displays the pages in the current section. This is not the first or second time I have used this recipe so it's time to share. This block can be used to display other pages in a section.

drop down and subnav
As we see in this image, there is a drop down menu and a block that displays the same items in the drop-down as a block on the right.

nested menu
Only one menu is needed. There is no need to maintain two menus or a custom block and a main menu.

Here is the code for the block. It retrieves the menu as an array. If the current path you are on is a descendant of a top level menu item, the top item will have "in_active_trail" set to TRUE. We are interested only in the menu items below, so we retrieve those and theme them. No extra rules need to be added on when to display the block. The block will not display if there is no output. If you are in a path that is not in the menu, the block will not show.

<?php
  $tree = menu_tree_page_data('primary-links');  //substitute your menu name for primary-links
  foreach($tree as $key => $mi) {
      if ($mi['link']['in_active_trail'] && $tree[$key]['below']) {    
        $menu = menu_tree_output($tree[$key]['below']);                  
      }
  }
  print $menu;
?>

None
Login or register to tag items
Your rating: None Average: 6.6 (5 votes)

Why isn't this part of the Menu module?

Thanks for this code snippet. I've added to my Drupal site, works beautifully. It preserves links for further levels, which is nice.

Still seems strange that Drupal's Menu functionality doesn't implement this. I think this would be a common feature. Maybe a checkbox, "Disable Top Nav Items", etc.

Just an update, I've found

Just an update, I've found several modules that extend this functionality and package it up very nice.

Menu Block: http://drupal.org/project/menu_block

This one works fantastic.
And on the project page there's a list of related projects.

warning

I'm not sure why, but applying this code to our drupal site broke everything... might it be a version issue?
it seems to have been having a hard time with the reference to "menu_tree_page_data"

luckily we were able to revert to the previous state

A word of warning: back your stuff up before trying this.

@Logosfera That code snippet

@Logosfera

That code snippet goes in a block via the admin interface.

There's no need to worry about breaking the loop when you find what you need. You'll never have more than a few top level menu items to loop through anyway.

A little optimization

You should use 'continue;' to stop the loop as soon as you find something.

Where do you put that code? In a theme template file or in a block via the admin interface?
I'm new to drupal so I'm sorry if the question seems stupid :)

Syndicate content

©1999 - 2010 LevelTen Interactive - Dallas, TX