2

I want to display an array of users of my site in a view.

How do I show if a user is logged-in or offline? I can not find anything to do that in sight.

8
  • 1
    Isn't there a View in core that does this? Whos Online?
    – Kevin
    Jul 10, 2017 at 16:02
  • @Kevin No I can not find fields "online" in my view
    – ZenImagine
    Jul 10, 2017 at 16:14
  • There is a Who's Online view...
    – Kevin
    Jul 10, 2017 at 16:16
  • @Kevin Can you show me how it looks
    – ZenImagine
    Jul 10, 2017 at 16:19
  • It's in Views, it's called "Who's Online"
    – Kevin
    Jul 10, 2017 at 16:20

4 Answers 4

1

You can check $user->getLastAccessedTime() in a preprocess function and set this as a variable, for example the view at /admin/structure/views/view/who_s_online determines if someone is "online" by checking if last accessed time is within the last 15 minutes.

function mymodule_preprocess_views_view(&$variables) {

  if ($variables['view']->id() == 'my_example_view') {
    /** @var $account \Drupal\user\UserInterface */
    $account = <load up your user>

    if ($account->isAuthenticated() && time() - $account->lastAccessedTime() < 900) {
      $variables['online'] = ['#markup' => 'online'];
    }
    else {
      $variables['online'] = ['#markup' => 'not online'];
    }
  }
}

And then to use 'online', you'd need to use a custom template for views-view-my-example-view.html.twig.

If you want to check a bunch of users, it may make more sense to use a view mode and use the preprocess function for the view mode.

The main point of this answer isn't how to theme views, but how to determine "online" as best as possible using Drupal.

5
  • Thank you, but it's a shame that drupal does not offer anything to show users in real time
    – ZenImagine
    Jul 10, 2017 at 17:58
  • 1
    @ZenImagine : Drupal is open source, just submit an appropriate patch to get it added to it ... Jul 10, 2017 at 18:58
  • @Pierres.Vriens I do not have the level to submit a patch
    – ZenImagine
    Jul 10, 2017 at 19:04
  • The question is for a view. Are you sure that adding $variables['online'] is enough to show that markup? The template file used from Drupal core doesn't contain {{ online }}.
    – apaderno
    Jul 11, 2017 at 7:07
  • I have updated the answer to be more specific to views.
    – oknate
    Jul 11, 2017 at 14:40
0

Probably not useful for 8, but in 7 you can take a look at user.module on lines 1428-1480 (version 7.56), and this chunk in particular appears to be responsible for generating this list:

 // Display a list of currently online users.
        $max_users = variable_get('user_block_max_list_count', 10);
        if ($authenticated_count && $max_users) {
          $items = db_query_range('SELECT u.uid, u.name, MAX(s.timestamp) AS max_timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= :interval AND s.uid > 0 GROUP BY u.uid, u.name ORDER BY max_timestamp DESC', 0, $max_users, array(':interval' => $interval))->fetchAll();
          $output .= theme('user_list', array('users' => $items));
0

I believe Drupal doesn't store an "online" status for users nor does it record logouts (Because it doesn't make sense. I can logout by deleting browser cookies and no one would know).

However, it does keep timestamps of last login (when the user last logged in) and last access (when the user accessed the site while logged in). You can probably create a view of users and their last access timestamp, filtering it down to the timestamps that fall within the past hour or something.

2
  • I want to display the user's status on each profile. So a filter will not work.
    – ZenImagine
    Jul 10, 2017 at 16:22
  • Actually the filter does work, because if you look at the core View for Whos Online, it determines this list based on who has been logged in in the last 15 minutes, which is about as good as it will get (while still being somewhat performant). You will need a filter anyway by UID to drop it on the user page, with the altered View. With the View isolated by UID, you can lower the timestamp value to say, 120 seconds, which is closer to 'real time'.
    – Kevin
    Jul 11, 2017 at 15:01
0
$db_conn = \Drupal::database();
$query = $db_conn->select('sessions', 'se');
$query->fields('se', ['uid']);
$result = $query->execute()->fetchAll(\PDO::FETCH_ASSOC);
$users = [];
foreach($result as $res){
  if($res['uid'] != 0){
    $users[$res['uid']] = $res['uid'];
  }
}

You can user this query and get all logged in users in array. But this will show only users with active session.

3
  • @A Ajay Reddy Thanks, but I am looking to view all users with on their profile the word "Online" or "Offline".
    – ZenImagine
    Jul 10, 2017 at 16:44
  • 2
    You're talking in circles and it isn't making sense. The core "Whos Online" view will do this without any code. You can modify that to accept a UID argument from the URL to isolate it to that user.
    – Kevin
    Jul 10, 2017 at 16:55
  • As kevin said there is view for who's online , you can find that view in "YOURSITENAME/admin/structure/views/view/who_s_online"
    – Ajay Reddy
    Jul 10, 2017 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.