Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Display in menu only if logged in

Started by Muut Archive 9 years ago · 3 replies · 2982 views
9 years ago

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.

9 years ago

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

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

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

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

---twig

<ul class="navigation">
{% if theme_config.dropdown.enabled %}
{{ _self.loop(pages) }}
{% else %}
{% for page in pages.children.visible %}
{% set current_page = (page.active or page.activeChild) ? 'active' : '' %}

TWIG
        {# //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 %}

</ul>

👍 1

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1332 9 years ago
Archive · by Muut Archive, 9 years ago
2 923 9 years ago
Archive · by Muut Archive, 9 years ago
2 4053 9 years ago
Archive · by Muut Archive, 9 years ago
1 2931 9 years ago
Archive · by Muut Archive, 9 years ago
3 1109 9 years ago