0

Previously, in my Drupal 8 contrib theme, I was rendering the full menu tree as:

function gratis_preprocess_page(&$vars, $hook) {

// Build the main menu links tree.
  $vars['primary_nav'] = FALSE;
  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 a hardcoded default values.
    $main_links_source = $menu_enabled ? $config->get('main_links') : 'main';
    $vars['main_menu'] = menu_tree($main_links_source);
    $vars['main_menu']['#theme_wrappers'] = array('menu_tree__primary');
    $vars['main_menu']['#attributes']['id'] = 'main-menu-links';
    $vars['main_menu']['#attributes']['class'] = array('links', 'clearfix');
  }
}

However, in testing the theme with the latest Drupal 8 dev, I am getting a WSOD with this specific error:

PHP Fatal error:  Call to undefined function menu_tree() in /themes/gratis/gratis.theme on line 93

Line 93 from the above is:

$vars['main_menu'] = menu_tree($main_links_source);

I did a little searching and discovered this core change: Convert menu tree building to a service. and its corresponding change notice.

I tried adjusting my code to:

$vars['main_menu']= \Drupal::service($main_links_source);

... but now I am getting this error:

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "main". Did you mean one of these: "maintenance_mode_subscriber", "user_maintenance_mode_subscriber"? in Symfony\Component\DependencyInjection\Container->get() (line 303 of core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Container.php).

So I am at a loss of how to fix this. You can see the repo for my .theme file here.

Cross referencing Render full menu tree in Drupal 8 version.

Update: [working code]

Here is the code in its fixed and working version thanks once again to @Clive

// 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 a 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');
  }

and then we render the wrapper set above with menu_tree__primary

/**
 * Theme wrapper function for the primary menu links.
 */
function gratis_menu_tree__primary(&$vars) {
  return '<ul class="toggle-menu">' . $vars['tree'] . '</ul>';
}
  • The service is named menu_link.tree, not the string you've got in $main_links_source (Just on mobile, can't flesh this out at the moment). Suffice to say, use the correct service name and you'll get the menu link service class - from there you'll be able to render a tree using one of its methods – Clive Apr 18 '14 at 13:31
3

Does https://drupal.org/node/2226481 help you?

This should be something like

\Drupal::service('menu_link.tree')->renderMenu($main_link_source);
  • No that does not work -- I already linked to that change notice above in my question. If I try \Drupal::service('menu_link.tree')->renderMenu($main_links_source); and then change if (!empty($vars['menu_link.tree'])) { using menu_link.tree, the menu now renders but without the submenus still. – Danny Englander Apr 18 '14 at 13:46
  • @DannyEnglander The error in your question comes from using the wrong service name in your code...if you're using different code could you update the question? – Clive Apr 18 '14 at 14:49
  • Either way you might be looking for the MenuTree::renderTree() method rather than renderMenu() – Clive Apr 18 '14 at 14:51
  • @Clive - see "Update" above. I added my latest code which is only partially working but not rendering the sub-menu items. – Danny Englander Apr 19 '14 at 1:05
  • 1
    @DannyEnglander Seems to work for the Gratis theme too: imgur.com/LkIWBpD – Clive Apr 19 '14 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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