Skip to content

Instantly share code, notes, and snippets.

@henrytran9x
Created January 3, 2017 09:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrytran9x/41af67d74bf5406b7178d650e8ca32eb to your computer and use it in GitHub Desktop.
Save henrytran9x/41af67d74bf5406b7178d650e8ca32eb to your computer and use it in GitHub Desktop.
Render menu navigation menu Drupal 8
<?php
function render_menu_navigation($menu_name,$theme_alter = ''){
//Set system menu mobile
$menu_tree = \Drupal::menuTree();
// Build the typical default set of menu tree parameters.
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$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);
// Finally, build a renderable array from the transformed tree.
$menu = $menu_tree->build($tree);
if(!empty($theme_alter)){
$menu['#theme'] = $theme_alter;
}
return \Drupal::service('renderer')->render($menu);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment