menu_block.sort.inc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * @file
  4. * Provides optional sorting of the active trail in the menu tree.
  5. */
  6. /**
  7. * Sort the active trail to the top of the tree.
  8. *
  9. * @param $tree
  10. * array The menu tree to sort.
  11. * @return
  12. * void
  13. */
  14. function _menu_tree_sort_active_path(&$tree) {
  15. // To traverse the original tree down the active trail, we use a pointer.
  16. $current_level =& $tree;
  17. // Traverse the tree along the active trail.
  18. do {
  19. $next_level = $sort = $first_key = FALSE;
  20. foreach ($current_level AS $key => &$value) {
  21. // Save the first key for later use.
  22. if (!$first_key) {
  23. $first_key = $key;
  24. }
  25. if ($current_level[$key]['link']['in_active_trail'] && $current_level[$key]['below']) {
  26. // Don't re-sort if its already sorted.
  27. if ($key != $first_key) {
  28. // Create a new key that will come before the first key.
  29. list($first_key, ) = explode(' ', $first_key);
  30. $first_key--;
  31. list(, $new_key) = explode(' ', $key, 2);
  32. $new_key = "$first_key $new_key";
  33. // Move the item to the new key.
  34. $current_level[$new_key] = $current_level[$key];
  35. unset($current_level[$key]);
  36. $key = $new_key;
  37. $sort = TRUE; // Flag sorting.
  38. }
  39. $next_level = $key; // Flag subtree.
  40. break;
  41. }
  42. }
  43. // Sort this level.
  44. if ($sort) {
  45. ksort($current_level);
  46. }
  47. // Continue in the subtree, if it exists.
  48. if ($next_level) {
  49. $current_level =& $current_level[$next_level]['below'];
  50. }
  51. } while ($next_level);
  52. }