Hook menu

How do you make a menu into a structure? The path attribute creates the structure.

Suppose you have the following page structure:
aa
aa/bbb
aa/bbb/cc
aa/dd
xx
xx/yy
xx/yy/zzz

You want a menu that starts with:
aa
xx

You want the menu to expand as you click on entries.

The trick is the path attribute in the item returned through hook_menu. If you write a module named example_module then you create your menu through a function named example_module_menu that returns an array containing menu items with each item containing an attribute named path. hook_menu magically decodes the path structure in each item and builds the menu structure from those path structures.

To build the structure shown above, you would start with the following code. Note that I left out useful attributes including weight and callback so we can focus on path.

$items[] = array('path' => 'aa', 'title' => t('Aa'));
$items[] = array('path' => 'aa/bbb', 'title' => t('Bbb'));
$items[] = array('path' => 'aa/bbb/cc', 'title' => t('Cc'));
$items[] = array('path' => 'aa/dd', 'title' => t('Dd'));
$items[] = array('path' => 'xx', 'title' => t('Xx'));
$items[] = array('path' => 'xx/yy', 'title' => t('Yy'));
$items[] = array('path' => 'xx/yy/zzz', 'title' => t('Zzz'));

menu.inc

includes/menu.inc builds the menu from the items array. As of 4.7.2, menu.inc contains _menu_build() to gather the items from modules and build the menu. If path is missing, _menu_build() sets path to an empty string. If type is missing, _menu_build() sets type to MENU_NORMAL_ITEM. If weight is missing, _menu_build() sets weight to zero.

Manual definition of parent

peterx - December 16, 2006 - 02:38

See Allow manual specification of parent. If you feel comfortable modifying includes/menu.inc then you can build a structure manually by specifying the parent of an item with $item['parent'] = 'xxxxxxx'. The change is proposed for Drupal 6.0.

The parent must precede the child as menu.inc builds the parent list sequentially. If taxonomy/term/5 is the parent of taxonomy/term/34 then, when you create 34, add 'parent' => 'taxonomy/term/5' to the item for 34.

petermoulding.com/web_architect

 
 

Drupal is a registered trademark of Dries Buytaert.