Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
D8 : Load menu entries from menu and menu_root
<?php
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
class Controller extends ControllerBase {
function loadAdminMenuEntries($menu_root, $menu_name = 'admin') {
$menu_items = [];
$parameters = \Drupal::menuTree()
->getCurrentRouteMenuTreeParameters($menu_name)->setRoot($menu_root);
$tree = \Drupal::menuTree()->load($menu_name, $parameters);
if ($tree[$menu_root]->hasChildren) {
$subtree = $tree[$menu_root]->subtree;
foreach ($subtree as $item) {
$menu_items[] = [
'title' => $item->link->getTitle(),
'description' => $item->link->getDescription(),
'url' => $item->link->getUrlObject()->toString(),
];
}
}
return $menu_items;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.