Saturday, 30 May 2015

How to retrieve all the items of a menu tree and display with customized UI in Drupal 6

We can use the menu funtion menu_tree_all_data(), which war available in file.  Need to pass the menu name with prefix of 'menu-' as a paramenter for this function.
Ex :
Let us create one menu with name as "test-menu".  And create few items in it, such as Menu1, Menu2, Menu3 as a sub link of Menu1 (Menu1--->Menu3). Now, whenever, we click on that menu under site building -> Menu section, the url would be with "menu-test-menu" as a last argument.  We need pass this one as a paramenter to the function menu_tree_all_data() as follows:
$menu_tree = menu_tree_all_data("menu-test-menu");
It would return an array which has  main menus (such as Menu1, Menu2 ) as keys, and values consists of array with two key-value pairs, keys are "link", "below". Here, the key "link" would associate with details of current menu which includes a key 'has_children', it would be '1' if the current menu has sub-links.  And the key "below" would associate with the sub-links details if any.
function get_sitemap() {

$menu_items = menu_tree_all_data('menu-test-menu');
foreach($menu_items as $key => $value) {
$output .= "<a href='".url($value['link']['link_path'])."' title='".$value['link']['link_title']."' >".$value['link']['link_title']."</a><br/>";
if($value['link']['has_children']) {
$output = add_child_menus($value['below'],$output);
return $output;
function add_child_menus($child_menus = array(),$output = null) {
foreach ($child_menus as $child_key => $child_value) {
$output .= "<a style='margin-left:".$child_value['link']['depth']."0px' href='".url($child_value['link']['link_path'])."' title='".$child_value['link']['link_title']."' >".$child_value['link']['link_title']."</a><br/>";
if($child_value['link']['has_children']) {
$output = add_child_menus($child_value['below'],$output);
return $output;
Here, in the get_sitemap() function, checking whether menu item has children or not, if TRUE, calling the recursive function add_child_menus() with the value associated with "below" key and current string.  Function add_child_menus() would recheck each and every sublink and associated links and returns the final output which has all menus with corresponding sublinks.  We can add classes in the anchor tags and place respective CSS.

No comments:

Post a Comment

Your comment is so valuable as it would help me in my growth of knowledge.