Skip to content

Instantly share code, notes, and snippets.

@chloecorfmat
Last active September 16, 2020 08:11
Show Gist options
  • Save chloecorfmat/181e78547ad40dbe5614853da0baf5d1 to your computer and use it in GitHub Desktop.
Save chloecorfmat/181e78547ad40dbe5614853da0baf5d1 to your computer and use it in GitHub Desktop.
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