SHARE
TWEET

Drupal 8 Menu tree

highrockmedia Aug 12th, 2014 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Override or insert variables into the page template.
  3.  */
  4. function gratis_preprocess_page(&$vars, $hook) {
  5. // main menu tree with sub-links.
  6.   if (!empty($vars['main_menu'])) {
  7.     $menu_tree = \Drupal::menuTree();
  8.     $menu_enabled = Drupal::moduleHandler()->moduleExists('menu');
  9.     $config = Drupal::config('menu.settings');
  10.     $menu_name = $menu_enabled ? $config->get('main_links') : 'main';
  11.     $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
  12.     $parameters->setMaxDepth($level + 1);
  13.     $tree = $menu_tree->load($menu_name, $parameters);
  14.     $manipulators = array(
  15.       array('callable' => 'menu.default_tree_manipulators:checkAccess'),
  16.       array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
  17.       array(
  18.         'callable' => 'menu.default_tree_manipulators:extractSubtreeOfActiveTrail',
  19.         'args' => array($level)
  20.       ),
  21.     );
  22.     $tree = $menu_tree->transform($tree, $manipulators);
  23.     return $menu_tree->build($tree);
  24.     $vars['main_menu']['#attributes']['id'] = 'main-menu-links';
  25.     $vars['main_menu']['#attributes']['class'] = array('links', 'clearfix');
  26.     // set the theme menu function for use below.
  27.     $vars['main_menu']['#theme_wrappers'] = array('menu_tree__primary');
  28.   }
  29. }
  30.  
  31. /**
  32.  * Theme wrapper function for the primary menu links.
  33.  */
  34. function gratis_menu_tree__primary(&$vars) {
  35.   return '<ul class="toggle-menu"></ul>';
  36. }
  37.  
  38. // Then in page.html.twig, we render the menu
  39.  
  40.     {% if main_menu %}
  41.      {{ main_menu }}
  42.    {% endif %}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top