1

I am trying to achieve is move active class from element to parent

  • element, this should be possible to do in .twig template (I wouldn't like that) so how to get menu in Drupal 8 and alter it's attributes?

    {#  https://api.drupal.org/api/drupal/core!modules!system!templates!menu.html.twig/8
    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.#}
    
    {# All menu and submenu items #}
    
    <div class="collapse navbar-collapse" id="main-navigation">
        <ul class="nav navbar-right navbar-nav">
    
            {% for item in items %}
    
                {# Menu with submenu #}
    
                {% if item.below %}
    
                    <li  {{ item.attributes }} >
                        <a href="{{ item.url }}" class="dropdown-toggle" data-toggle="dropdown" > {{ item.title }} <b class="caret"></b> </a>
    
                        <ul class="dropdown-menu">
                            {# Menu -> Submenu #}
    
                            {% for item in item.below %}
    
                                <li ><a href="{{ item.url }}" {{ item.attributes }} > {{ item.title }} </a></li>
    
                            {% endfor %}
                        </ul>
                    </li>
                    {# Menu without submenu #}
    
                {% else %}
    
                    {# Menu - Link #}
    
                    <li {{ item.attributes }} ><a  href="{{ item.url }}"  {{ item.attributes }} > {{ item.title }}</a></li>
    
                {% endif %}
    
            {% endfor %}
    
        </ul>
    </div>
    

    The above code is menu--main.html.twig file,hear attributes not working.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Browse other questions tagged or ask your own question.