Alen Saqe Alen Saqe - 9 months ago 177
Twig Question

Print second level menu in Drupal 8

Drupal 8 markup changed quite a lot from Drupal 7 using the Twig Engine. We are developing a site with it. We want to print the second level menu links in there.

{% if main_menu %}
<nav id ="main-menu" class="navigation" role="navigation">
{{ main_menu }}
</nav> <!-- /#main-menu -->

This is how we print the menu in Drupal 8. A syntax im not quite used to.

How can i print. Second level links on the menu. Or the Menu Tree?


According to template_preprocess_page(), you should have access to secondary_menu as well, so:

{% if secondary_menu %}
  <nav id ="secondary-menu" class="navigation" role="navigation">
      {{ secondary_menu }}
{% endif %}

Generally speaking, you can use a preprocess hook to add variables. In this case that would be something like:

function MYTHEME_preprocess_page(&$vars) {
  $vars['foo'] = 'bar'; // foo is available in the page template file