Output children menus of collapsed elements

Last modified: August 26, 2009 - 23:33

Description

This snippet will allow you to configure Drupal to output the children menus of "collapsed" elements, still keeping the "collapsed" style on the parents. After this change is performed, creating hover menus is a breeze.

Step 1 of 2

Add these two methods to your template.php file

<?php
function phptemplate_menu_tree($pid = 1) {
    return
_phptemplate_callback('menu_tree', array('pid' => $pid));
}

function
phptemplate_menu_item($mid, $children = '', $leaf = TRUE) {
    return
_phptemplate_callback('menu_item', array('mid' => $mid, 'children' => $children, 'leaf' => $leaf));
}
?>

Step 2 of 2

Create two new files in your theme folder, the first called menu_tree.tpl.php and the second called menu_item.tpl.php

menu_tree.tpl.php

<?php

  $menu
= menu_get_menu();
 
$output = '';

  if (isset(
$menu['visible'][$pid]) && $menu['visible'][$pid]['children']) {
    foreach (
$menu['visible'][$pid]['children'] as $mid) {
     
$type = isset($menu['visible'][$mid]['type']) ? $menu['visible'][$mid]['type'] : NULL;
     
$children = isset($menu['visible'][$mid]['children']) ? $menu['visible'][$mid]['children'] : NULL;
     
$output .= theme('menu_item', $mid,
       
menu_in_active_trail($mid) || (true) ? //This is the line that changed, "true" was added.
         
theme('menu_tree', $mid) : '', count($children) == 0);
    }
  }

  echo
"\n<ul class=\"menu\">\n". $output ."\n</ul>\n";
?>

menu_item.tpl.php

<?php
  $menu
= menu_get_menu();
 
$expanded = menu_in_active_trail($mid) || ($menu['visible'][$mid]['type'] & MENU_EXPANDED);
  echo
'<li class="'. ($leaf ? 'leaf' : ($expanded ? 'expanded' : 'collapsed')) .'">'. menu_item_link($mid) . $children ."</li>\n";
?>

 
 

Drupal is a registered trademark of Dries Buytaert.