Wednesday, December 22, 2010

How to display sub menus (children) of primary links in drupal?

Step 1 : Since we'll be using php codes in a block to display menu, the first thing to do is to enable the PHP Filter under modules.
Step 2 : Go to administer->block->add block and create a new block. Add a description (i use submenu). Choose PHP Code as the filter and paste the code below in the body textarea.

$menuItems = menu_tree_page_data('primary-links'); 

foreach($menuItems as $key => $m) {
      //check current active menu
   if ($m['link']['in_active_trail'] && $menuItems [$key]['below']) {
       $menu = menu_tree_output($menuItems [$key]['below']);

//print the menu
print $menu;
Make sure you include the php delimiters in your code. Press save.
Step 3 : Place your block in a region you want. and that's it. next time you select a menu item in the primary links, the children menu items will show in this block.
You can use active-trail to style the selected menu item.

Alternatively, you may use Menu Block module which can do the same without writing a single line of code. Let me know if you need any guidence.

No comments: