Add menu sub-navigation as a sidebar block in Drupal
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.
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.
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.
$mi) {
if ($mi['link']['in_active_trail'] && $tree[$key]['below']) {
$menu = menu_tree_output($tree[$key]['below']);
}
}
print $menu;
?>