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.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI 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.
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.
$variables['online']
is enough to show that markup? The template file used from Drupal core doesn't contain {{ online }}
.
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));
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.
$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.