Score:0

Get class on item.below (child item of menu) inside
  • tag in menu--main.html.twig
  • ru flag

    Hi I can't insert class in child item of main menu and remove existing. Part of my code looks like this:

    {% for item in items %}
      {%
        set item_classes = [
        'menu-item-edot',
        item.is_expanded ? 'dropdown',
        item.is_collapsed ? '',
        item.in_active_trail ? 'is-active',
      ]
      %}
      <li{{ item.attributes.addClass(item_classes) }}>
    
        {%
          set link_classes = [
          'nav-link',
          item.in_active_trail ? '',
        ]
          %}
    
        {{ link(item.title, item.url, attributes.addClass(link_classes)) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes.removeClass('nav-link'), menu_level + 1)  }}
        {% endif %}
      </li> 
    

    So all li tags return:

    <li class="menu-item-edot">
    

    But I need just top menu items return upper code and all other child menu items to return:

    <li class="dropdown-item">
    

    I didn't find way to remove class "menu-item-edot" and add "dropdown-item" to menu child items.

    Please help

    sonfd avatar
    in flag
    Use the `menu_level` variable to determine level and set the classes you want.
    Score:0
    ru flag

    Thank you sonfd this my code now and it's working!

    {% for item in items %}
    
      {% if menu_level == 0 %}
    
        {%
          set item_classes = [
          'menu-item-edot',
          item.is_expanded ? 'dropdown',
          item.is_collapsed ? '',
          item.in_active_trail ? 'is-active',
        ]
        %}
    
        <li{{ item.attributes.addClass(item_classes) }}>
    
        {% elseif  menu_level == 1 %}
    
          {%
            set item_classes = [
            'dropdown-item',
            item.is_expanded ? 'dropdown',
            item.is_collapsed ? '',
            item.in_active_trail ? 'is-active',
          ]
          %}
    
          <li{{ item.attributes.addClass(item_classes) }}>
      {% endif %}
        {%
          set link_classes = [
          'nav-link',
          item.in_active_trail ? '',
        ]
          %}
    
        {{ link(item.title, item.url, attributes.addClass(link_classes)) }}
        {% if item.below %}
    
          {{ menus.menu_links(item.below, attributes.removeClass('nav-link'), menu_level + 1)  }}
    
        {% endif %}
      </li>
    
    mangohost

    Post an answer

    Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.