highrockmedia

Drupal 8 Menu tree

Aug 12th, 2014
105
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×