You are here

menu--field-content.html.twig in Menu item content fields 8

Theme implementation to display a menu with fields.

It is a very similar implementation to the system module template except for the following code that will render the menu item as an entity when is available.


{% if item.content %}
  {{ item.content }}
{% else %}
  {{ link(item.title, item.url) }}
{% endif %}

When the menu.html.twig template is overriden in your theme this piece of code needs to be added.

File

templates/menu--field-content.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Theme implementation to display a menu with fields.
  5. *
  6. * It is a very similar implementation to the system module template
  7. * except for the following code that will render the menu item
  8. * as an entity when is available.
  9. *
  10. * @code
  11. * {% if item.content %}
  12. * {{ item.content }}
  13. * {% else %}
  14. * {{ link(item.title, item.url) }}
  15. * {% endif %}
  16. * @endcode
  17. *
  18. * When the menu.html.twig template is overriden in your theme
  19. * this piece of code needs to be added.
  20. */
  21. #}
  22. {% import _self as menus %}
  23. {#
  24. We call a macro which calls itself to render the full tree.
  25. @see http://twig.sensiolabs.org/doc/tags/macro.html
  26. #}
  27. {{ menus.menu_links(items, attributes, 0) }}
  28. {% macro menu_links(items, attributes, menu_level) %}
  29. {% import _self as menus %}
  30. {% if items %}
  31. {% if menu_level == 0 %}
  32. <ul{{ attributes }}>
  33. {% else %}
  34. <ul>
  35. {% endif %}
  36. {% for item in items %}
  37. <li{{ item.attributes }}>
  38. {% if item.content %}
  39. {{ item.content }}
  40. {% else %}
  41. {{ link(item.title, item.url) }}
  42. {% endif %}
  43. {% if item.below %}
  44. {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
  45. {% endif %}
  46. </li>
  47. {% endfor %}
  48. </ul>
  49. {% endif %}
  50. {% endmacro %}