Drupal - Check if the user is logged-in in a theme

If you just to check the current user is logged-in, you can use $variables['logged_in'], which is generally available in all the template files.

For example, the mark.html.twig file uses the following code, although the only documented variable is status.

{% if logged_in %}
  {% if status is constant('MARK_NEW') %}
    <span class="marker">{{ 'New'|t }}</span>
  {% elseif status is constant('MARK_UPDATED') %}
    <span class="marker">{{ 'Updated'|t }}</span>
  {% endif %}
{% endif %}

The variable is explicitly documented in other template files, such as html.html.twig, page.html.twig, and node.html.twig.

The variable is available in all the template files, since it is initialized in _template_preprocess_default_variables() that invokes user_template_preprocess_default_variables_alter() (an implementation of hook_template_preprocess_default_variables_alter()), which contains the following code.

  $user = \Drupal::currentUser();

  $variables['user'] = clone $user;
  // Remove password and session IDs, since themes should not need nor see them.
  unset($variables['user']->pass, $variables['user']->sid, $variables['user']->ssid);

  $variables['is_admin'] = $user->hasPermission('access administration pages');
  $variables['logged_in'] = $user->isAuthenticated();

_template_preprocess_default_variables() is called by template_preprocess(), which is the function called for theme hooks implemented as templates; this guarantees the variable is available in all the template files.

Keep in mind that macros don't have access to the current template variables, so trying to access logged_in in the code of a macro would not have any effect.
Between the template files used from Drupal core modules, the ones using a macro are:

  • menu.html.twig

    {% macro menu_links(items, attributes, menu_level) %}
      {% import _self as menus %}
      {% if items %}
        {% if menu_level == 0 %}
          <ul{{ attributes }}>
        {% else %}
          <ul>
        {% endif %}
        {% for item in items %}
          <li{{ item.attributes }}>
            {{ link(item.title, item.url) }}
            {% if item.below %}
              {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
            {% endif %}
          </li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endmacro %}
    
  • book-tree.html.twig

    {% macro book_links(items, attributes, menu_level) %}
      {% import _self as book_tree %}
      {% if items %}
        {% if menu_level == 0 %}
          <ul{{ attributes }}>
        {% else %}
          <ul>
        {% endif %}
        {% for item in items %}
          <li{{ item.attributes }}>
            {{ link(item.title, item.url) }}
            {% if item.below %}
              {{ book_tree.book_links(item.below, attributes, menu_level + 1) }}
            {% endif %}
          </li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endmacro %}
    
  • menu--toolbar.html.twig

    {% macro menu_links(items, attributes, menu_level) %}
      {% import _self as menus %}
      {% if items %}
        {% if menu_level == 0 %}
          <ul{{ attributes.addClass('toolbar-menu') }}>
        {% else %}
          <ul class="toolbar-menu">
        {% endif %}
        {% for item in items %}
          {%
            set classes = [
              'menu-item',
              item.is_expanded ? 'menu-item--expanded',
              item.is_collapsed ? 'menu-item--collapsed',
              item.in_active_trail ? 'menu-item--active-trail',
            ]
          %}
          <li{{ item.attributes.addClass(classes) }}>
            {{ link(item.title, item.url) }}
            {% if item.below %}
              {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
            {% endif %}
          </li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endmacro %}
    

For example, changing the last macro with the following code would not have the expected result.

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    {% if menu_level == 0 %}
      <ul{{ attributes.addClass('toolbar-menu') }}>
    {% else %}
      <ul class="toolbar-menu">
    {% endif %}
    {% for item in items %}
      {%
        set classes = [
          'menu-item',
          logged_in ? 'menu-item--logged-in-user',
          item.is_expanded ? 'menu-item--expanded',
          item.is_collapsed ? 'menu-item--collapsed',
          item.in_active_trail ? 'menu-item--active-trail',
        ]
      %}
      <li{{ item.attributes.addClass(classes) }}>
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      </li>
    {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}

You can with the Twig Extender module. Quote from its project page:

Add a simple plugin system to add new twig extensions (Filter and Functions). Provides a new service provider for "twig.extensions" to add new plugins.

Function: is_user_logged_in

Check if user is logged in.

{% if user_is_logged_in() %}
Hello user
{% else %}
Please login
{% endif %}

Tags:

Theming

8