Add menu sub-navigation as a sidebar block in Drupal


By Dustin Currie - Posted on 18 June 2009

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;
?>

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

Another great information for installing side bar block with Drupal software.

Radio Airtime

The possibility of a full bladder causing an erection, especially during sleep is perhaps further supported by the beneficial physiological effect of an erection inhibiting urination,thereby helping to avoid nocturnal enuresis.
Regards,
Erecciones

The male reproductive system creates sperm that is manufactured in the seminiferous tubules within each testicle.
Regards,
Sperme

This results primarily from its excellent and proven results.We are so confident that our product works, we will, without any kind of questions that return your money if you are not satisfied with the results achieved.
Regards,
Dr Marie- Sperma

Alternative medicine and natural products are about to conquer the marks.This will definitely give you the best results and need no time to wait for using it.
Regards,
Dr Strikk- penisverlängerung

The breasts of a female primate’s body contain the mammary glands, which secrete milk used to feed infants.
Regards,
Dr Dukes- brustvergrösserung

Back pain in individuals with medical conditions that put them at high risk for a spinal fracture such as osteoporosis or multiple myelitis,also warrants prompt medical attention.
Regards,
back ache

This is an efficient way to add a sub-navigation side bar menu.

Gift

The Schoenhut My First Piano is table top piano that has 18 keys and comes fully assembled. It is a model that has been a staple in the Schoenhut line for years.thanks for sharing the information.
Regards,
Schoenhut My First Piano

I was on a transcription job before when I first heard about "how to start your own website". The transcription job was actually a webinar about the different things you need to put into account when creating your own website. This is the first time as well that I have encountered the terms design and usability. I didn't actually get it until now as I was reading through this post that this was what the webinar was referring about. This input is very important especially when you're putting into account the type of readers for your site. As much possible you want to stick to the basics at the same time not compromising functionality, design, and usability.

Farmville Secrets

The information you provided is very useful, actually you have saved us months of trying new methods.
Drupal is a very powerful platform, I can easily imagine you doing 2-level drop down navigation and a sidebar block that displays the pages in the current section on it.
I think I will try your way & be back to you if I have any further questions.
Thanks
anonymous surfing

thanks for the code.I ve applied it to my drupal site.It is working fine.Thanks a lot again.
reseller web hosting

What's the efficient way to add a navigation menu?
Seller in Cheapest Heating oil

Nice post!It will really gives the crucial moments for others to know about it. It is a great passion for the people to select the great categories of the stories.........

5.11 tactical pants

a spinal fracture such as osteoporosis or multiple myelitis,also warrants prompt medical attention.
Second Hand Cars

Thanks. some great information here keep up the good work.
Astaga.com Lifestyle on The Net

Good Info here.That is the main way for the people to know about the good types of techniques to equip it. It will really gives the crucial moments for others to know about it. It is a great passion for the people to select the great categories of the stories on the requirements of using it.

Meal Replacement Shakes

Zoids Genesis is the fourth anime installment of the Zoids franchise.It tells the story of Ruuji Familon a young boy who finds a Liger-type Zoid during a salvage mission.
Regards,
Online TV Streaming

A local celebrity can be more of a household name than a national celebrity and may often experience the same type of attention from the public as a national celebrity thanks for sharing the nice info..
Regards,
Jackson wiles - Celeb Oops

The only option for the user who wants to enable DMA mode is to uninstall and reinstall the device.

Discount Gift Cards

Thanks. some great information here keep up the good work.I cannot really leave a more constructive comment as i’m a bit out of my depth but i will be checking back here for further updates.

Car LED Lights

It retrieves the menu as an array. If the current path you are on is a descendant of a top level menu items.
find address from phone number

Thanks for the great post.

Free Image Hosting

cool.. Thanks for this information. You made the instructions clearly that's why it is easy to follow. It's much appreciated! Best regards.

Scotland Photos

No extra rules need to be added on when to display the block.
Loriannz

Good Info here.That is the main way for the people to know about the good types of techniques to equip it. It will really gives the crucial moments for others to know about it. It is a great passion for the people to select the great categories of the stories on the requirements of using it.Extra Bucks

The only option for the user who wants to enable DMA mode is to uninstall and reinstall the device.
daewoo parts

The only option for the user who wants to enable DMA mode is to uninstall and reinstall the device.
mazda parts

7

Realy very good tutorial, it helped he a lot. Thanks Dustin
____________
cialis soft

Thanks. some great information here keep up the good work.I cannot really leave a more constructive comment as i’m abit out of my deph but i will be checking back here for further updates.

Colonial Utah

I have used this recipe so it's time to share. This block can be used to display other pages in a section.
Physiotherapists

Have you finished your project successfully?

Thesis Statement | Research Proposal | Doctoral Dissertation

Thanks for the information

Vitiligo

Thanks. some great information here keep up the good work.I cannot really leave a more constructive comment as i’m abit out of my deph but i will be checking back here for further updates.Quality Top Site
Golden Casino Games
General WordPress Weblog
Personal Blog
Idahoagbell

Nice menu's module for this. It's awesome and the menus look great. Thanks for sharing.

las vegas escorts

cool.. Thanks for this information. You made the instructions clearly that's why it is easy to follow. It's much appreciated! Best regards.

auto parts

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

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 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.

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 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.

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 :)

Request a Quote

Contact Us Tel: 866.277.9958
4228 N. Central Expressway
Suite 210
Dallas, TX 75206
Get your free quote!

NEWSLETTER SIGN UP

Upcoming Events

03/12/2010 - 12:00am - 03/16/2010 - 12:00pm
04/13/2010 - 12:00am - 04/15/2010 - 12:00pm
04/19/2010 - 12:00am - 04/21/2010 - 12:00pm