You are here

public function Loader::loadMenu in Bamboo Twig 8

Returns the render array for Drupal menu.

Parameters

string $menu_name: The name of the menu.

int $level: (optional) Initial menu level.

int $depth: (optional) Maximum number of menu levels to display.

Return value

array A render array for the menu.

File

bamboo_twig_loaders/src/TwigExtension/Loader.php, line 213

Class

Loader
Provides a 'Loader' Twig Extensions.

Namespace

Drupal\bamboo_twig_loaders\TwigExtension

Code

public function loadMenu($menu_name, $level = 1, $depth = 0) {
  $parameters = $this->menuTree
    ->getCurrentRouteMenuTreeParameters($menu_name);

  // Adjust the menu tree parameters based on the block's configuration.
  $parameters
    ->setMinDepth($level);

  // When the depth is configured to zero, there is no depth limit. When depth
  // is non-zero, it indicates the number of levels that must be displayed.
  // Hence this is a relative depth that we must convert to an actual
  // (absolute) depth, that may never exceed the maximum depth.
  if ($depth > 0) {
    $parameters
      ->setMaxDepth(min($level + $depth - 1, $this->menuTree
      ->maxDepth()));
  }
  $parameters
    ->onlyEnabledLinks();
  $parameters->expandedParents = [];
  $tree = $this->menuTree
    ->load($menu_name, $parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $this->menuTree
    ->transform($tree, $manipulators);
  return $this->menuTree
    ->build($tree);
}