How to display the status of the user? I created a TWIG template for the "Teaser" view of the user account.

I want to display a logo and change its color according to the status of the user :


When the user is logged in &lt;i class="user-online fa fa-circle fa-lg"&gt;&lt;/i&gt; Online
When the user is logged in and the last access is less than 30 minutes &lt;i class="user-absent fa fa-circle fa-lg"&gt;&lt;/i&gt; Absent
When the user is offline &lt;i class="user-offline fa fa-circle fa-lg"&gt;&lt;/i&gt; Offline


How to display the status of the user ?

I think it must be possible to create conditions in TWIG
you can use template_preprocess_user  like 



 /**
 * Implements hook_entity_presave().
 */
function mytemplate_preprocess_user(&$variables) {
  // get user object
  $user = $variables['elements']['#user'];
  //- The user has logged in at least once
  if ($user->getLastLoginTime()) {
    if (account_is_logged_in_less_then_thirty_minutes($user->id())) {
      $status = 'Online';
    }
    else {
      $status = 'Absent';
    }
  }
  else {
    $status = 'offline';
  }
  $variables['status'] = $status;
}

/**
 * @param $uid
 *
 * @return bool
 */
function account_is_logged_in_less_then_thirty_minutes($uid) {
  $connection = Database::getConnection();
  $query = $connection->select('sessions', 'sessions')
    ->fields('sessions', ['sid', 'uid', 'timestamp'])
    ->condition('sessions.uid', $uid, '=')
    //- chef if the user was online in 30 minutes (60 * 30)
    ->condition('sessions.timestamp', \Drupal::time()
        ->getRequestTime() - (60 * 30), '>')
    ->execute();
  //- Get result.
  $results = $query->fetchAll(\PDO::FETCH_OBJ);
  return (count($results) > 0) ? TRUE : FALSE;
}



In you template check on status variable like the following:

{% if status == 'Online' %}
  &lt;i class="user-online fa fa-circle fa-lg"&gt;&lt;/i&gt; Online
{% elseif status == 'Absent' %}
  &lt;i class="user-absent fa fa-circle fa-lg"&gt;&lt;/i&gt; Absent
{% else %}
  &lt;i class="user-offline fa fa-circle fa-lg"&gt;&lt;/i&gt; Offline
{% endif %}


Add use Drupal\Core\Database\Database; to the top of your file.