Add 'active_trail' class to a 'menu_tree_all_data' result


/ Published in: PHP
Save to your folder(s)

Via: [http://www.touchdesk.nl/2009/04/full-menu-tree-with-active-trail-in-drupal-6/](http://www.touchdesk.nl/2009/04/full-menu-tree-with-active-trail-in-drupal-6/)


Copy this code and paste it in your HTML
  1. ...
  2. $data = menu_find_active_trail(menu_tree_all_data(...));
  3. ...
  4.  
  5. /**
  6.  * Wrapper function
  7.  */
  8. function menu_find_active_trail(&$menu_tree) {
  9. $item = menu_get_item();
  10. _menu_find_active_trail($menu_tree, $item);
  11. return $menu_tree;
  12. }
  13.  
  14. /**
  15.  * Recursive function to find the active menu and the active trail in the given tree.
  16.  */
  17. function _menu_find_active_trail(&$menu_tree, $item) {
  18. $level_is_expanded = FALSE;
  19. foreach($menu_tree as &$menu_item) {
  20. $link = &$menu_item['link'];
  21. if ($link['href']==$item['href']) { // Found the exact location in the tree
  22. $link['active'] = TRUE;
  23. $link['in_active_trail'] = TRUE;
  24. return true;
  25. } else {
  26. if ($link['has_children']) {
  27. $result = _menu_find_active_trail($menu_item['below'], $item);
  28. $link['in_active_trail'] = $result;
  29. if ($result) $level_is_expanded = TRUE;
  30. }
  31. }
  32. }
  33. return $level_is_expanded;
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.