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.6 (7 votes)
None
Login or register to tag items

That's a handy little tutorial to add a flavor to ur page. Thanks.

Nicolas Cage Movies
http://NicolasCagemovies.org/

7

This site is called Bid for Bargain. Why? You are bid for a specific item that you like for a cheap cost. Just visit our website www.bid4bargain.com. Click Bids if you wanna know more about the different bids of different items. Click Auctions to know what are the different items that you can get. Click Bargains to know what are the different bargains we have.

Try it! You will not waste your time visiting this site.

BlockBuster Deals - the BARGAIN pOst
This is also very important!! Great SAVINGS AND PRICES AT ALL LOW COSTS!
I like the website which sells all the goods and properties at a low cost. All should come and visit this!!!!

Bid 4 Bargain- USA Auctions Inc.
Auction, Bargains, Bids, Good and properties for sale
The online auctions market place to sell all your goods and properties.
Real estate foreclosures auctions, FIND DEALS, MaKE MONEY, NYC
THIS WEBSITE YOU CAN MAKE MONEY OFF OF!
Bids and Auctions

Nice informative post.

USA Auctions Inc. - The online auctions market place to sell all your goods and properties. Everything you need and want is in this site. Come visit us and you won’t be disappointed.

Bargains

This shall add some life to the page. Aesthetically pleasing.

George Clooney Movies
http://GeorgeClooneymovies.org/

The menu addendum would look great on the web page at the same time functional.

Thanks for the post. Here's a variation for a subnav with a heading that only shows if the page is a part of a section.

  //traverses the menu tree array to produce subnav with heading from parent and children links
  $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']) {     	   
        $title = $mi['link']['link_title'];
    	print '<div class="intBox"><div class="intBoxSubnav"><h2>'.$title.'</h2></div>';
    	$menu = menu_tree_output($tree[$key]['below']);  
  	}
  }  
  if ($title){
  		print '<div class="box">'.$menu.'</div></div><!--closes intBox -->';		
  }

These are very good tips, I think I will need this for my next web design project.
You have illustrated these information in a very easy, straight & step by step instructions.
This will make it very easy to implement.

5.04

There has been a good designs and would like to get some new ones and attractive templates.
Regards.

Shares rebounded in Hong Kong and China on Thursday as investors picked up Chinese banks after their recent sell-off, while China XD Electric became the first mainland stock in recent years to end its debut below its IPO price.

The complication is that intermediate goods, such as partially completed computers, are not consistently included in the scope of the term.Regards.

I love how Drupal software lets you work around your site in a thousand different ways. It actually allows you to put a lot of personality and uniqueness into your own site without compromising usability.

The sub-navigation sidebar works perfect for me. Thanks!

With the advent of powerful desktop computers,highly realistic pianos have become available as affordable software modules.

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

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

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.

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.

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

What's the efficient way to add a navigation menu?

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.

Thanks. some great information here keep up the good work.

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.

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

Tp

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.

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

Thanks for the great post.

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

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

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.

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

especially during sleep is perhaps further supported by the beneficial physiological effect of an erection inhibiting urination,thereby helping to avoid nocturnal enuresis.video promo

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.

A cartoon series is a set of regularly presented animated television programs created or adapted for television broadcast with a common series title usually related to one another.
Regards,
Watch Series

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
03/16/2010 - 6:30pm - 8:30pm
03/23/2010 - 6:00pm - 9:00pm
03/23/2010 - 6:00pm - 8:00pm