Drupal 8 - "Implement menu links as plugins", trying to build a menu tree 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(&amp;$vars, $hook) {
// Build the main menu links tree.
  if (!empty($vars['main_menu'])) {
    // Build links.
    $config = Drupal::config('menu.settings');
    $menu_enabled = Drupal::moduleHandler()-&gt;moduleExists('menu');
    // When menu module is not enabled, we need hardcoded default values.
    $main_links_source = $menu_enabled ? $config-&gt;get('main_links') : 'main';
    $vars['main_menu'] = \Drupal::service('menu_link.tree')-&gt;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')-&gt;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-&gt;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')-&gt;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.
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()-&gt;moduleExists('menu_ui');
    // When menu module is not enabled, we need hardcoded default values.
    $main_links_source = $menu_enabled ? $config-&gt;get('main_links') : 'main';
    // Allowed max depth.
    // @see MenuTreeStorage::MAX_DEPTH
    $level = 9;
    $menu_tree = \Drupal::menuTree();
    $parameters = $menu_tree-&gt;getCurrentRouteMenuTreeParameters($main_links_source);
    $parameters-&gt;setMaxDepth($level);
    $tree = $menu_tree-&gt;load($main_links_source, $parameters);
    $manipulators = array(
      array('callable' =&gt; 'menu.default_tree_manipulators:checkAccess'),
      array('callable' =&gt; 'menu.default_tree_manipulators:generateIndexAndSort'),
    );
    $tree = $menu_tree-&gt;transform($tree, $manipulators);
    $vars['main_menu'] = $menu_tree-&gt;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.