You are here

function menu_tree_depth_trim in Menu Block 6.2

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

Prune a tree so it does not extend beyond the specified depth limit.

Parameters

$tree: array The menu tree to prune.

$depth_limit: int The maximum depth of the returned tree; must be a positive integer.

Return value

void

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

File

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

Code

function menu_tree_depth_trim(&$tree, $depth_limit) {

  // Prevent invalid input from returning a trimmed tree.
  if ($depth_limit < 1) {
    return;
  }

  // Examine each element at this level to find any possible children.
  foreach (array_keys($tree) as $key) {
    if ($tree[$key]['below']) {
      if ($depth_limit > 1) {
        menu_tree_depth_trim($tree[$key]['below'], $depth_limit - 1);
      }
      else {

        // Remove the children items.
        $tree[$key]['below'] = FALSE;
      }
    }
    if ($depth_limit == 1 && $tree[$key]['link']['has_children']) {

      // Turn off the menu styling that shows there were children.
      $tree[$key]['link']['has_children'] = FALSE;
      $tree[$key]['link']['leaf_has_children'] = TRUE;
    }
  }
}