Created
January 3, 2017 09:06
-
-
Save henrytran9x/41af67d74bf5406b7178d650e8ca32eb to your computer and use it in GitHub Desktop.
Render menu navigation menu Drupal 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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