public function UserOnlineBlock::build

Builds and returns the renderable array for this block plugin.

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockRenderController

File

drupal/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php, line 81
Contains \Drupal\user\Plugin\Block\UserOnlineBlock.

Class

UserOnlineBlock
Provides a "Who's online" block.

Namespace

Drupal\user\Plugin\Block

Code

public function build() {

  // Count users active within the defined period.
  $interval = REQUEST_TIME - $this->configuration['seconds_online'];

  // Perform database queries to gather online user lists.
  $authenticated_count = db_query("SELECT COUNT(uid) FROM {users} WHERE access >= :timestamp", array(
    ':timestamp' => $interval,
  ))
    ->fetchField();
  $build = array(
    '#theme' => 'item_list__user__online',
    '#prefix' => '<p>' . format_plural($authenticated_count, 'There is currently 1 user online.', 'There are currently @count users online.') . '</p>',
  );

  // Display a list of currently online users.
  $max_users = $this->configuration['max_list_count'];
  if ($authenticated_count && $max_users) {
    $uids = db_query_range('SELECT uid FROM {users} WHERE access >= :interval AND uid > 0 ORDER BY access DESC', 0, $max_users, array(
      ':interval' => $interval,
    ))
      ->fetchCol();
    foreach (user_load_multiple($uids) as $account) {
      $build['#items'][] = theme('username', array(
        'account' => $account,
      ));
    }
  }
  return $build;
}