You are here

function _menu_tree_sort_active_path in Menu Block 7.3

Same name and namespace in other branches
  1. 6.2 menu_block.sort.inc \_menu_tree_sort_active_path()
  2. 7.2 menu_block.sort.inc \_menu_tree_sort_active_path()

Sort the active trail to the top of the tree.

Parameters

$tree: array The menu tree to sort.

Return value

void

1 call to _menu_tree_sort_active_path()
menu_tree_sort_active_path in ./menu_block.module
Sort the active trail to the top of the tree.

File

./menu_block.sort.inc, line 15
Provides optional sorting of the active trail in the menu tree.

Code

function _menu_tree_sort_active_path(&$tree) {

  // To traverse the original tree down the active trail, we use a pointer.
  $current_level =& $tree;

  // Traverse the tree along the active trail.
  do {
    $next_level = $sort = $first_key = FALSE;
    foreach ($current_level as $key => &$value) {

      // Save the first key for later use.
      if (!$first_key) {
        $first_key = $key;
      }
      if ($current_level[$key]['link']['in_active_trail'] && $current_level[$key]['below']) {

        // Don't re-sort if its already sorted.
        if ($key != $first_key) {

          // Create a new key that will come before the first key.
          list($first_key, ) = explode(' ', $first_key);
          $first_key--;
          list(, $new_key) = explode(' ', $key, 2);
          $new_key = "{$first_key} {$new_key}";

          // Move the item to the new key.
          $current_level[$new_key] = $current_level[$key];
          unset($current_level[$key]);
          $key = $new_key;
          $sort = TRUE;

          // Flag sorting.
        }
        $next_level = $key;

        // Flag subtree.
        break;
      }
    }

    // Sort this level.
    if ($sort) {
      ksort($current_level);
    }

    // Continue in the subtree, if it exists.
    if ($next_level) {
      $current_level =& $current_level[$next_level]['below'];
    }
  } while ($next_level);
}