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.
<?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; ?>
- Dustin Currie's blog
- Login or register to post comments

An Abercrombie And Fitch
An Abercrombie And Fitch employee in northern California is alleging she was fired for refusing to remove her hijab, or headscarf marking her Muslim observance. Abercrombie pursuit of a homogeneous army of "perfect" employees appears to have snared it again!
Abercrombie Clothing to the AP, "the Council on Abercrombie UK Relations said Wednesday it filed an Equal Abercrombie London Opportunity Commission complaint on behalf of Hani Khan." Abercrombie Fitch says she was told she would be allowed to wear a Abercrombie Outlet, but a visiting district manager disputed that. She says she was fired when she refused to take it off.
In 2008, an Abercrombie And Fitch accused Abercrombie of refusing to hire her because her head scarf "didn't fit the chain's image." That lawsuit, filed last year, is still in progress.
Abercrombie in the company's serious on-the-ground sensitivity issues: Abercrombie Clothing to let a woman help her autistic sister try on Abercrombie UK, for which they were fined $115,264, and banishing an employee with a prosthetic arm from the store floor. That employee, Riam Dean, was awarded £8,000 for unlawful harassment, although the tribunal ruled that she hadn't suffered disability discrimination.
Abercrombie London has a well-documented mission of selling its idea of youthful physical perfection, Abercrombie Fitch the Bruce Weber ad campaigns to the employees that fit its ideal of American beauty. The company conceded that that ideal didn't include black, Abercrombie Outlet, and Asian employees in 2004 when it paid $40 million to employees and job applicants of those demographics to settle a class-action federal discrimination lawsuit. They had been accused of "engaging in recruiting and hiring practices that exclude minorities and adopting a virtually all-white marketing campaign."
ed hardy ed hardy
ed hardy clothing ed hardy clothing
ed hardy shop ed hardy shop
cheap ed hardy cheap ed hardy
ed hardy uk ed hardy uk
ed hardy sale ed hardy sale
ed hardy clothes ed hardy clothes
ed hardy outlet ed hardy outlet
ed hardy ed hardy
ed hardy clothing ed hardy clothing
ed hardy shop ed hardy shop
cheap ed hardy cheap ed hardy
ed hardy uk ed hardy uk
ed hardy sale ed hardy sale
ed hardy clothes ed hardy clothes
ed hardy outlet ed hardy outlet
In this case, the user cannot
In this case, the user cannot turn on DMA for this device. The only option for the user who wants to enable DMA mode is to uninstall and reinstall the device.
get degree AND PhD history AND BS degree
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 :)