For more information about this repository, visit the project page at https://www.drupal.org/project/twig_tweak
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.1 KiB
71 lines
2.1 KiB
5 years ago
|
<?php
|
||
|
|
||
|
namespace Drupal\twig_tweak\View;
|
||
|
|
||
|
use Drupal\Core\Menu\MenuLinkTreeInterface;
|
||
|
|
||
|
/**
|
||
|
* MenuViewBuilder service.
|
||
|
*/
|
||
|
class MenuViewBuilder {
|
||
|
|
||
|
/**
|
||
|
* The menu link tree service.
|
||
|
*
|
||
|
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
|
||
|
*/
|
||
|
protected $menuLinkTree;
|
||
|
|
||
|
/**
|
||
|
* Constructs a MenuViewBuilder object.
|
||
|
*/
|
||
|
public function __construct(MenuLinkTreeInterface $menu_link_tree) {
|
||
|
$this->menuLinkTree = $menu_link_tree;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the render array for a menu.
|
||
|
*
|
||
|
* @param string $menu_name
|
||
|
* The name of the menu.
|
||
|
* @param int $level
|
||
|
* (optional) Initial menu level.
|
||
|
* @param int $depth
|
||
|
* (optional) Maximum number of menu levels to display.
|
||
|
* @param bool $expand
|
||
|
* (optional) Expand all menu links.
|
||
|
*
|
||
|
* @return array
|
||
|
* A render array for the menu.
|
||
|
*
|
||
|
* @see \Drupal\system\Plugin\Block\SystemMenuBlock::build()
|
||
|
*/
|
||
|
public function build(string $menu_name, int $level = 1, int $depth = 0, bool $expand = FALSE): array {
|
||
|
$parameters = $this->menuLinkTree->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->menuLinkTree->maxDepth()));
|
||
|
}
|
||
|
|
||
|
// If expandedParents is empty, the whole menu tree is built.
|
||
|
if ($expand) {
|
||
|
$parameters->expandedParents = [];
|
||
|
}
|
||
|
|
||
|
$tree = $this->menuLinkTree->load($menu_name, $parameters);
|
||
|
$manipulators = [
|
||
|
['callable' => 'menu.default_tree_manipulators:checkAccess'],
|
||
|
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
|
||
|
];
|
||
|
$tree = $this->menuLinkTree->transform($tree, $manipulators);
|
||
|
return $this->menuLinkTree->build($tree);
|
||
|
}
|
||
|
|
||
|
}
|