logo

In views template how can check user is logged in drupal 8

14
March

In views template how can check user is logged in drupal 8
By: Anonymous | Published On: Tue, 03/14/2023 - 18:20

In a Drupal 8 views template, you can check if a user is logged in by using the following code

{% if logged_in %}
  Welcome, {{ user.name }}
{% else %}
  {{ path('user.login') }}
{% endif %}

Explanation:

  •     The logged_in variable is a boolean that is set to true if the user is logged in, and false if they are not.
  •     The user variable contains information about the currently logged-in user, such as their username (user.name), email address (user.mail), or user ID (user.uid).
  •     The path() function generates a URL for the user login page (user.login) so that the user can log in if they are not already logged in.

Note: The logged_in variable and user variable are only available in certain views templates, such as views-view.html.twig and views-view-fields.html.twig. If you're using a different views template, these variables may not be available, so be sure to check the documentation for the specific template you're using.

 

Need Help ?