Skip to content

Instantly share code, notes, and snippets.

@karlosgliberal
Created May 11, 2016 14:51
Show Gist options
  • Select an option

  • Save karlosgliberal/bbad0d1af4f1dd41e98b70009241eb56 to your computer and use it in GitHub Desktop.

Select an option

Save karlosgliberal/bbad0d1af4f1dd41e98b70009241eb56 to your computer and use it in GitHub Desktop.
/**
* Provides function to programmatically rendering a menu
*
* @param String $menu_name
* The machine configuration id of the menu to render
*/
public function render_menu(string $menu_name) {
$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);
return array('#markup' => drupal_render($menu));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment