Skip to content
Snippets Groups Projects
menu--main.html.twig 4.74 KiB
Newer Older
Brian Canini's avatar
Brian Canini committed
{#
/**
 * @file
 * Default theme implementation to display a menu.
 *
 * Available variables:
 * - classes: A list of classes to apply to the top level <ul> element.
 * - dropdown_classes: A list of classes to apply to the dropdown <ul> element.
 * - 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.
 *
 * @ingroup templates
 *
 * Define a custom macro that will render all menu trees.
 */
#}



{% macro menu_links(items, attributes, menu_level, classes, issidebar) %}

  {% if issidebar == 'sidebar_menu' %}
    {# check block region name
    * if sidebar setup sidebar menu
    #}
    {% if items %}

      <ul{{ attributes.addClass(classes, 'sidebar-nav') }}>

      {% for item in items %}
        {%
          set item_classes = item.url.getOption('container_attributes').class | split(" ")
        %}
        {%
          set item_classes = [
            item.is_expanded and item.below ? 'expanded dropdown',
            item.in_active_trail ? 'active active-trail',
            loop.first ? 'first',
            loop.last ? 'last',
          ]
        %}
        <li{{ item.attributes.addClass(item_classes) }}>
          {% set link_title = item.title %}
          {% set link_attributes = item.link_attributes %}
          {# {% set link_attributes = link_attributes.addClass('dropdown-toggle').setAttribute('data-toggle', 'dropdown') %} #}

          {# Must use link() here so it triggers hook_link_alter(). #}
          {% if item.is_expanded and item.below %}
            <div class="link-wrapper">{{ link(link_title, item.url, link_attributes.addClass(item.in_active_trail ? 'active-trail')) }} <i class="fa fa-angle-right" aria-hidden="true"></i></div>
          {% else %}
          <div class="link-wrapper">{{ link(link_title, item.url, link_attributes.addClass(item.in_active_trail ? 'active-trail')) }}</div>
          {% endif %}
          {% if item.below %}
            {{ _self.menu_links(item.below, attributes.removeClass(classes), menu_level + 1, dropdown_classes) }}
          {% endif %}
        </li>
      {% endfor %}
      </ul>
    {% endif %}
    {# END SIDEBAR NAV
    #}
  {% else %}

{#
  NEED TO FIGURE OUT HOW TO
  check class of parent twig element
https://drupal.stackexchange.com/questions/297714/twig-find-if-a-css-class-exists-in-a-tag
https://stackoverflow.com/questions/52312728/accessing-the-referencing-parent-element-in-twig-paragraphs

  #}


    {% if items %}
        {# {% if isMobile() %}
          <ul{{ attributes.addClass(menu_level == 0 ? classes : dropdown_classes, 'mobileview') }} role="menubar">
        {% else %} #}
Brian Canini's avatar
Brian Canini committed
          <ul{{ attributes.addClass(menu_level == 0 ? classes : dropdown_classes, menu_level != 0 ? 'asc-submenu') }} {% if menu_level == 0 %}role="menubar"{% endif %}>
Brian Canini's avatar
Brian Canini committed
        {# {% endif %} #}
      {% for item in items %}
        {%
          set item_classes = item.url.getOption('container_attributes').class | split(" ")
        %}
        {%
          set item_classes = [
            item.is_expanded and item.below ? 'expanded dropdown',
            item.in_active_trail ? 'active active-trail',
            loop.first ? 'first',
            loop.last ? 'last',
            issidebar != 'sidebar_menu' ? 'bux-menu__item',
          ]
        %}
        <li{{ item.attributes.addClass(item_classes) }}>
          {% set link_title = item.title %}
          {% set link_attributes = item.link_attributes %}
          {# {% if menu_level == 0 and item.is_expanded and item.below %}
            {% set link_title %}{{ link_title }} <span class="caret"></span>{% endset %}
            {% set link_attributes = link_attributes.addClass('dropdown-toggle').setAttribute('data-toggle', 'dropdown') %}
          {% endif %} #}
          {# Must use link() here so it triggers hook_link_alter(). #}
Brian Canini's avatar
Brian Canini committed
          {{ link(link_title, item.url, link_attributes.addClass(item.in_active_trail ? 'active-trail', issidebar != 'sidebar_menu' and menu_level == 0 ? 'bux-menu__link')) }}
Brian Canini's avatar
Brian Canini committed
          {% if item.below %}
Brian Canini's avatar
Brian Canini committed
            {{ _self.menu_links(item.below, attributes.removeClass(classes), menu_level + 1) }}
Brian Canini's avatar
Brian Canini committed
          {% endif %}
        </li>
      {% endfor %}
      </ul>
    {% endif %}
  {% endif %}
{% endmacro %}
{#
  Invoke the custom macro defined above. If classes were provided, use them.
  This allows the template to be extended without having to also duplicate the
  code above. @see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{ _self.menu_links(items, attributes, 0, classes ?: ['menu', 'menu--' ~ menu_name|clean_class, 'nav'], issidebar ?: blockname) }}