You are here

function menu_tree_prune_tree in Menu Block 7.3

Same name and namespace in other branches
  1. 6.2 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

array $tree: The menu tree to prune.

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

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

1 call to menu_tree_prune_tree()
menu_tree_block_data in ./menu_block.module
Gets the data structure representing a menu tree for the given configuration.

File

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

Code

function menu_tree_prune_tree(array &$tree, $level, array $parent_item = NULL) {
  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 ($tree as $key => &$value) {
        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;
      }
    }
  }
  $is_front_page = drupal_is_front_page();

  // 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 ($tree as $key => &$value) {

      // Also include the children of the front page.
      if ($tree[$key]['link']['in_active_trail'] || $tree[$key]['link']['link_path'] == '<front>' && $is_front_page) {

        // 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;
    }
  }
}