I'm trying to render out a menu with its label in Drupal 8. I am able to get the menu and its links inside of a preprocess function using a helper function, but I can't locate the menu's title.
Here is how I'm getting the menu. I'm able to add the return value as a twig variable $variable['menu']['menu-name'] and render out an <ul>
anywhere I need to.
function get_menu($menu_name) {
$menu_tree = \Drupal::menuTree();
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
$tree = $menu_tree->load($menu_name, $parameters);
$manipulators = array(
// Only show links that are accessible for the current user.
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
// Use the default sorting of menu links.
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($tree, $manipulators);
$menu = $menu_tree->build($tree);
return array('#markup' => drupal_render($menu));
}
\Drupal::service('renderer')->renderRoot()
instead ofdrupal_render()
.