You are here

function menu_tree_prune_tree in Menu Block 6.2

Same name and namespace in other branches
  1. 7.3 menu_block.module \menu_tree_prune_tree()
  2. 7.2 menu_block.module \menu_tree_prune_tree()

Prune a tree so that it begins at the specified level.

This function will follow the active menu trail to the specified level.

Parameters

$tree: array The menu tree to prune.

$level: int The level of the original tree that will start the pruned tree.

$parent_item: array The menu item that should be used as the root of the tree.

Return value

void

1 call to menu_tree_prune_tree()
menu_tree_build in ./menu_block.module
Build a menu tree based on the provided configuration.

File

./menu_block.module, line 504
Provides configurable blocks of menu items.

Code

function menu_tree_prune_tree(&$tree, $level, $parent_item = FALSE) {
  if (!empty($parent_item)) {

    // Prune the tree along the path to the menu item.
    for ($i = 1; $i <= MENU_MAX_DEPTH && $parent_item["p{$i}"] != '0'; $i++) {
      $plid = $parent_item["p{$i}"];
      $found_active_trail = FALSE;

      // Examine each element at this level for the ancestor.
      foreach (array_keys($tree) as $key) {
        if ($tree[$key]['link']['mlid'] == $plid) {
          menu_block_set_title($tree[$key]['link']);

          // Prune the tree to the children of this ancestor.
          $tree = $tree[$key]['below'] ? $tree[$key]['below'] : array();
          $found_active_trail = TRUE;
          break;
        }
      }

      // If we don't find the ancestor, bail out.
      if (!$found_active_trail) {
        $tree = array();
        break;
      }
    }
  }

  // Trim the upper levels down to the one desired.
  for ($i = 1; $i < $level; $i++) {
    $found_active_trail = FALSE;

    // Examine each element at this level for the active trail.
    foreach (array_keys($tree) as $key) {
      if ($tree[$key]['link']['in_active_trail']) {

        // Get the title for the pruned tree.
        menu_block_set_title($tree[$key]['link']);

        // Prune the tree to the children of the item in the active trail.
        $tree = $tree[$key]['below'] ? $tree[$key]['below'] : array();
        $found_active_trail = TRUE;
        break;
      }
    }

    // If we don't find the active trail, the active item isn't in the tree we want.
    if (!$found_active_trail) {
      $tree = array();
      break;
    }
  }
}