5

In a previous Drupal 8 menu question here, we determined that menu tree building was now a service. In my D8 contrib theme, I was then able to render the main menu tree as:

function gratis_preprocess_page(&$vars, $hook) {
// Build the main menu links tree.
  if (!empty($vars['main_menu'])) {
    // Build links.
    $config = Drupal::config('menu.settings');
    $menu_enabled = Drupal::moduleHandler()->moduleExists('menu');
    // When menu module is not enabled, we need hardcoded default values.
    $main_links_source = $menu_enabled ? $config->get('main_links') : 'main';
    $vars['main_menu'] = \Drupal::service('menu_link.tree')->renderMenu($main_links_source);
    $vars['main_menu']['#attributes']['id'] = 'main-menu-links';
    $vars['main_menu']['#attributes']['class'] = array('links', 'clearfix');
    $vars['main_menu']['#theme_wrappers'] = array('menu_tree__primary');
  }
}

The key to this is the use of:

$vars['main_menu'] = \Drupal::service('menu_link.tree')->renderMenu($main_links_source);

This worked great to fix my main menu tree in Drupal 8 Alpha 11. However, there have been more sweeping changes to the menu system since then and now in Drupal 8.0.x Alpha14, this breaks again with the error:

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "menu_link.tree". Did you mean this: "menu.link_tree"? in Symfony\Component\DependencyInjection\Container->get() (line 303 of /Users/danny/Sites/Drupal/d8/drupal8/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Container.php). 

The thing that stands out from the above seems to be:

Did you mean this: "menu.link_tree"?

I tried

$vars['main_menu'] = \Drupal::service('menu.link_tree')->renderMenu($main_links_source);

... but that does not work either.

I filed a core issue here and that lead me to "[META] New plan, Phase 2: Implement menu links as plugins" and the very same change notice which I used to upgrade this code originally. Admittedly, I am pretty lost here.

0

1 Answer 1

6
+50

You may need to render it as below:

  // Build the main menu links tree.
  if (!empty($vars['main_menu'])) {
    // Build links.
    $config = Drupal::config('menu_ui.settings');
    $menu_enabled = Drupal::moduleHandler()->moduleExists('menu_ui');
    // When menu module is not enabled, we need hardcoded default values.
    $main_links_source = $menu_enabled ? $config->get('main_links') : 'main';
    // Allowed max depth.
    // @see MenuTreeStorage::MAX_DEPTH
    $level = 9;
    $menu_tree = \Drupal::menuTree();
    $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($main_links_source);
    $parameters->setMaxDepth($level);
    $tree = $menu_tree->load($main_links_source, $parameters);
    $manipulators = array(
      array('callable' => 'menu.default_tree_manipulators:checkAccess'),
      array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
    );
    $tree = $menu_tree->transform($tree, $manipulators);
    $vars['main_menu'] = $menu_tree->build($tree);
    $vars['main_menu']['#attributes']['id'] = 'main-menu-links';
    $vars['main_menu']['#attributes']['class'] = array('links', 'clearfix');
    $vars['main_menu']['#theme_wrappers'] = array('menu_tree__primary');
  }

P.S: Make sure you clear cache after update, as it looks like the cache-tag is aggressive and doesn't show same menu in all page at the first run.

6
  • You've copied and pasted this code from the change notices but I don't see how it applies practically to my original example above. Aug 12, 2014 at 17:50
  • Updated answer more specific to the code sample in the question.
    – vijaycs85
    Aug 12, 2014 at 23:41
  • Your code did not work, but I looked in the core menu.inc file and came up with this: pastebin.com/FCN55bT8 However, $level seems to be undefined so I am now getting the top level menu items but not sub-menus even though I have the top level set to expanded. I think this is going to be a tough one to fix and we may not end up solving this here and now. Aug 13, 2014 at 0:53
  • I just fixed the syntax error in my second code block. However the $depth part isn't working anything other than 1. I filled a core issue at drupal.org/node/2322585
    – vijaycs85
    Aug 16, 2014 at 14:55
  • ok I followed the issue, let's see what happens with that. Thank you. Aug 16, 2014 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.