Follow-up to
#2443361: Remove theme_book_link, make book tree align with MenuLinkTree build β
, where the need for this became obvious.
Problem/Motivation
The template file core/modules/system/templates/menu.html.twig
includes this twig macro:
{% macro menu_links(items, attributes, menu_level) %}
{% import _self as menus %}
{% if items %}
{% if menu_level == 0 %}
<ul{{ attributes.addClass('menu') }}>
{% else %}
<ul class="menu">
{% endif %}
{% for item in items %}
<li{{ item.attributes }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
This is very useful for rendering not only menus, but any tree of links. However, this macro is not currently reusable, as this file is actually a template.
We could all make good use of the code if we tackle this, both within core and contrib.
See documentation on twig macros: http://twig.sensiolabs.org/doc/tags/macro.html
Proposed resolution
Exact solution is up for discussion. But any solution must be re-usable by book-tree.html.twig and menu--toolbar.html.twig at the very least.
Remaining tasks
- Generalize the macro. Ensure backwards-compatibility with old version.
- Figure out where else we can use this in core. For now we know:
- core/modules/book/templates/book-tree.html.twig
- core/modules/system/templates/menu.html.twig
- core/modules/toolbar/templates/menu--toolbar.html.twig
- + core/themes/stable versions of the above
- core/themes/classy/templates/navigation/book-tree.html.twig
- core/themes/classy/templates/navigation/menu.html.twig
- core/themes/classy/templates/navigation/menu--toolbar.html.twig
- Make the changes on all those places.
User interface changes
None
API changes
None, adding a general core twig macro, which means a new tool for themers to use.
Beta phase evaluation