You are here

toolbar-themes--base-menu.html.twig in Toolbar Themes 8

{#
/**
 * Toolbar themes theme implementation to display a toolbar menu.
 *
 * Available variables:
 * - menu_name: The machine name of the menu.
 * - items: A nested list of menu items. Each menu item contains:
 *   - attributes: HTML attributes for the menu item.
 *   - below: The menu item child items.
 *   - title: The menu link title.
 *   - url: The menu link url, instance of \Drupal\Core\Url
 *   - localized_options: Menu link localized options.
 *   - is_expanded: TRUE if the link has visible children within the current
 *     menu tree.
 *   - is_collapsed: TRUE if the link has children within the current menu tree
 *     that are not currently visible.
 *   - in_active_trail: TRUE if the link is in the active trail.
 * - show_icon: bool to show or hide icon markup.
 *
 * @ingroup themeable
 */
#}
{%- import _self as menus -%}
{#
  We call a macro which calls itself to render the full tree.
  @see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{- menus.menu_links(items, attributes, 0, menu_name) -}}

{%- macro menu_links(items, attributes, menu_level, menu_name) -%}
  {%- import _self as menus -%}

  {% if items %}
    <ul
      {%- if menu_level == 0 -%}
        {{ attributes.addClass(['toolbar-menu', 'odd', 'menu-level-1', menu_name ? 'menu-name--' ~ menu_name|clean_class ]) }}
      {%- else %}
        class="toolbar-menu is-child {{ cycle(['odd', 'even'], menu_level) }} {{ 'menu-level-' ~ (menu_level + 1) }}"
      {%- endif -%}
    >

      {# Begin loop. #}
      {% for item in items %}

        {% if item.below is not empty %}
          {% set is_parent = true %}
        {% else %}
          {% set is_parent = false %}
        {% endif %}

        {%
          set item_classes = [
            'menu-item',
            is_parent ? 'is-parent',
            item.is_expanded ? 'menu-item--expanded',
            item.is_collapsed ? 'menu-item--collapsed',
            item.in_active_trail ? 'menu-tem--active-trail',
          ]
        %}

        {# We set an id on list items to provide context for aria attributes. #}
        <li{{ item.attributes.addClass(item_classes).setAttribute('id', 'mlid-' ~ item.title|render|clean_class)|without('role') }}>
          {{
            link(
              item.title,
              item.url
            )
          }}

          {%- if item.show_icon -%}
            {%- if item.icon_attributes -%}
              <i{{ item.icon_attributes.setAttribute('data-grunticon-embed', '') }}></i>
            {%- endif -%}
          {%- endif -%}

          {% if item.below %}
            {{
              menus.menu_links(
                item.below,
                attributes,
                menu_level + 1,
                menu_name
              )
            }}
          {% endif %}
        </li>

      {%- endfor -%}
      {# End loop. #}

    </ul>
  {% endif %}
{%- endmacro -%}

File

themes/base/templates/toolbar-themes--base-menu.html.twig
View source
  1. {#
  2. /**
  3. * Toolbar themes theme implementation to display a toolbar menu.
  4. *
  5. * Available variables:
  6. * - menu_name: The machine name of the menu.
  7. * - items: A nested list of menu items. Each menu item contains:
  8. * - attributes: HTML attributes for the menu item.
  9. * - below: The menu item child items.
  10. * - title: The menu link title.
  11. * - url: The menu link url, instance of \Drupal\Core\Url
  12. * - localized_options: Menu link localized options.
  13. * - is_expanded: TRUE if the link has visible children within the current
  14. * menu tree.
  15. * - is_collapsed: TRUE if the link has children within the current menu tree
  16. * that are not currently visible.
  17. * - in_active_trail: TRUE if the link is in the active trail.
  18. * - show_icon: bool to show or hide icon markup.
  19. *
  20. * @ingroup themeable
  21. */
  22. #}
  23. {%- import _self as menus -%}
  24. {#
  25. We call a macro which calls itself to render the full tree.
  26. @see http://twig.sensiolabs.org/doc/tags/macro.html
  27. #}
  28. {{- menus.menu_links(items, attributes, 0, menu_name) -}}
  29. {%- macro menu_links(items, attributes, menu_level, menu_name) -%}
  30. {%- import _self as menus -%}
  31. {% if items %}
  32. <ul
  33. {%- if menu_level == 0 -%}
  34. {{ attributes.addClass(['toolbar-menu', 'odd', 'menu-level-1', menu_name ? 'menu-name--' ~ menu_name|clean_class ]) }}
  35. {%- else %}
  36. class="toolbar-menu is-child {{ cycle(['odd', 'even'], menu_level) }} {{ 'menu-level-' ~ (menu_level + 1) }}"
  37. {%- endif -%}
  38. >
  39. {# Begin loop. #}
  40. {% for item in items %}
  41. {% if item.below is not empty %}
  42. {% set is_parent = true %}
  43. {% else %}
  44. {% set is_parent = false %}
  45. {% endif %}
  46. {%
  47. set item_classes = [
  48. 'menu-item',
  49. is_parent ? 'is-parent',
  50. item.is_expanded ? 'menu-item--expanded',
  51. item.is_collapsed ? 'menu-item--collapsed',
  52. item.in_active_trail ? 'menu-tem--active-trail',
  53. ]
  54. %}
  55. {# We set an id on list items to provide context for aria attributes. #}
  56. <li{{ item.attributes.addClass(item_classes).setAttribute('id', 'mlid-' ~ item.title|render|clean_class)|without('role') }}>
  57. {{
  58. link(
  59. item.title,
  60. item.url
  61. )
  62. }}
  63. {%- if item.show_icon -%}
  64. {%- if item.icon_attributes -%}
  65. <i{{ item.icon_attributes.setAttribute('data-grunticon-embed', '') }}></i>
  66. {%- endif -%}
  67. {%- endif -%}
  68. {% if item.below %}
  69. {{
  70. menus.menu_links(
  71. item.below,
  72. attributes,
  73. menu_level + 1,
  74. menu_name
  75. )
  76. }}
  77. {% endif %}
  78. </li>
  79. {%- endfor -%}
  80. {# End loop. #}
  81. </ul>
  82. {% endif %}
  83. {%- endmacro -%}