I copied the twig file from the user view in my theme.

I want to display:

  • "Online" if the user is logged in.

  • "Absent" if the user has not had any activity for more than 15 minutes.

  • "Offline" if the user is disconnected.

How to do this with twig ?

 * @file
 * Default theme implementation to present all user data.
 * This template is used when viewing a registered user's page,
 * e.g., example.com/user/123. 123 being the user's ID.
 * Available variables:
 * - content: A list of content items. Use 'content' to print all content, or
 *   print a subset such as 'content.field_example'. Fields attached to a user
 *   such as 'user_picture' are available as 'content.user_picture'.
 * - attributes: HTML attributes for the container element.
 * - user: A Drupal User entity.
 * @see template_preprocess_user()
 * @ingroup themeable
<article{{ attributes.addClass('user--comment') }}>

  <div class="row">

    {{ content.user_picture }}
    {{ user.label }}

    {% if logged_in %}
      <i class="user-online fa fa-circle fa-lg"></i> En ligne
    {% else %}
      <i class="user-offline fa fa-circle fa-lg"></i> Hors ligne
    {% endif %}

    <div class="bs-field-link">
      <a href="/profile/{{ content.personnel_profiles.0 }}" title="{{ user.label }}"></a>



The preceding code displays the status of the current user.

For example for comments I want to display the status of the author of the comment (not the node). I have this line of code in the comments twig file.

Can I reuse the code to obtain the status of the author of the comment ?

{{ drupal_entity('user', comment.uid.target_id, 'comment') }}
  • 1
    There is a View that comes with core Drupal 8 that does this. You could make it a block and put it in the theme on user profile URLs. If you cloned it and added a contextual filter of User ID from URL, it would basically do what you want. The view is called "Whos Online?"
    – Kevin
    Apr 19, 2018 at 19:06
  • @Kevin Can this be done only with twig? The status will also be displayed on the authors of the comments. The url of the node will not match the author of the comment. Before I used EVA but I prefer to use twig.
    – user84727
    Apr 19, 2018 at 19:44
  • I can't think of any real time way to show this information and since you are talking comments, that sounds like it could be a lot of queries per page. I was initially thinking just 1. I would skip the "Absent"/idle part, and just query the user table by UID for 'last access' within 15 minutes. If you get a hit, they're online, if not, they're offline.
    – Kevin
    Apr 19, 2018 at 19:50
  • @Kevin I updated my question. With this code I know if the current user is connected. But this is not the author of the comment
    – user84727
    Apr 19, 2018 at 21:03
  • I can’t really tell what you’re doing. Isn’t the UID available in the comment twig templates?
    – Kevin
    Apr 19, 2018 at 21:37

1 Answer 1


You can't know if the person is behind its pc, reading the website but you can know the last access of the user (last time he done an http request to the site I suppose, load a page, submit a comment, etc, but I'm not totally sure about that). You need to look the property "access" of the UserSession.

For example, for the current user, the code is:

$session_current_user = \Drupal::currentUser()->getAccount();
$last_access = $session_current_user->getLastAccessedTime();

The property is a timestamp. You need convert if you want display it on you're site. So, you can now calculate when you considere your user Absent and determine after how many time you considere your user Offline. You can do all these calculation in your template.php file and used your results in your twig file.