You are here

protected function DepthSetting::menu_tree_depth_trim in Menu Bean 7

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 DepthSetting::menu_tree_depth_trim()
DepthSetting::alterTree in lib/Drupal/menu_bean/Setting/DepthSetting.php
Alters the menu tree

File

lib/Drupal/menu_bean/Setting/DepthSetting.php, line 62
Level Plugin class

Class

DepthSetting

Namespace

Drupal\menu_bean\Setting

Code

protected 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 ($tree as $key => &$value) {
    if ($tree[$key]['below']) {
      if ($depth_limit > 1) {
        $this
          ->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;
    }
  }
}