The "is-active" class is added to the main-menu links only when the site is accessed by an anonymous user

kr flag

I use this simple template (menu--main.html.twig) to render the main menu I created.

    {% for item in items %}
        {{ link(item.title, item.url) }}
        {% if item.below is not empty %}
            <div class='submenu'>
                {% for subitem in item.below %}
                    {{ link(subitem.title, subitem.url) }}
                {% endfor %}
        {% endif %}
    {% endfor %}

Drupal is nice enough to add an is-active class to the currently active link, which is good and worked fine for a bit.

Recently, it stopped working properly and I have no idea why. Currently it is only applying an is-active class to the menu links when I am not logged in. When I log in, there is only a menu link that gets that class applied to it when active.

The menu links are identical, apart title and destination on the Edit Menu Link menu, so I'm very confused.

Can anybody offer suggestions for things to try to help solve this problem?

ru flag
`is-active` is not rendered server-side in your Twig template, but added client-side with JavaScript. If it stopped working then you quite likely have javascript errors on your site.
Bradley avatar
kr flag
Thank you, as the site is not JavaScript heavy I didn't think to look into that but it turns out that was indeed the problem!
kr flag

The problem was with JavaScript, at some point I overwrote the html.html.twig template in order to add a container div immediately after the body tag, which subsequently wrapped this line:

<js-bottom-placeholder token="{{ placeholder_token }}">

Moving this one line outside of my container div but still within the body tag fixed the issue.

This article was useful


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.