Display in menu only if logged in

Hi, looking through the documentation i cant really figure out how to display pages only if the user has logged in. I’m using the default team grav “login” plugin.

One example would be if:

User is not logged in, display sign in page in menu
User has logged in, display page “My account” instead of the sign in page

I guess it has to be done using visibility and the site.login tag somehow.

This is the twig we have in Antimatter to display the option to logout:

    {% if config.plugins.login.enabled and grav.user.username %}
        <li><i class="fa fa-lock"></i> {% include 'partials/login-status.html.twig' %}</li>
    {% endif %}

Thanks!

Using @rhukster, I was able to hack the Antimatter navigation to hide the menu items i do now want shown by default.

The navigation section of the following file is below:
user/themes/antimatter/templates/partials/navigation.html.twig 

—twig

    {% if theme_config.dropdown.enabled %} {{ _self.loop(pages) }} {% else %} {% for page in pages.children.visible %} {% set current_page = (page.active or page.activeChild) ? 'active' : '' %}
        {# //show the menu if either the plugin is disabled (which it shouldnt be), if the site.login access is unset, or the user is logged in #}
        {% if (not config.plugins.login.enabled) or (not page.header.access['site.login']) or (grav.user.username) %}
          <li class="{{ current_page }}">
              <a href="{{ page.url }}">
                  {% if page.header.icon %}<i class="fa fa-{{ page.header.icon }}"></i>{% endif %}
                  {{ page.menu }}
            </a>  
        </li>
        {% endif %}

    {% endfor %}
{% endif %}
{% for mitem in site.menu %}
    <li>
        <a href="{{ mitem.url }}">
            {% if mitem.icon %}<i class="fa fa-{{ mitem.icon }}"></i>{% endif %}
            {{ mitem.text }}
        </a>
    </li>
{% endfor %}
{% if config.plugins.login.enabled and grav.user.username %}
    <li><i class="fa fa-lock"></i> {% include 'partials/login-status.html.twig' %}</li>
{% else %}
    <li> <a href="register">Register</a></li>
    <li> <a href="login">Login</a></li>
{% endif %}

1 Like