class UserOnlineBlock

Provides a "Who's online" block.

@todo Move this block to the Statistics module and remove its dependency on {users}.access.

Plugin annotation


@Plugin(
  id = "user_online_block",
  admin_label = @Translation("Who's online"),
  module = "user"
)

Hierarchy

Expanded class hierarchy of UserOnlineBlock

File

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

Namespace

Drupal\user\Plugin\Block
View source
class UserOnlineBlock extends BlockBase {

  /**
   * Overrides \Drupal\block\BlockBase::settings().
   */
  public function settings() {
    return array(
      'properties' => array(
        'administrative' => TRUE,
      ),
      'seconds_online' => 900,
      'max_list_count' => 10,
    );
  }

  /**
   * Overrides \Drupal\block\BlockBase::access().
   */
  public function access() {
    return user_access('access content');
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockForm().
   */
  public function blockForm($form, &$form_state) {
    $period = drupal_map_assoc(array(
      30,
      60,
      120,
      180,
      300,
      600,
      900,
      1800,
      2700,
      3600,
      5400,
      7200,
      10800,
      21600,
      43200,
      86400,
    ), 'format_interval');
    $form['user_block_seconds_online'] = array(
      '#type' => 'select',
      '#title' => t('User activity'),
      '#default_value' => $this->configuration['seconds_online'],
      '#options' => $period,
      '#description' => t('A user is considered online for this long after they have last viewed a page.'),
    );
    $form['user_block_max_list_count'] = array(
      '#type' => 'select',
      '#title' => t('User list length'),
      '#default_value' => $this->configuration['max_list_count'],
      '#options' => drupal_map_assoc(array(
        0,
        5,
        10,
        15,
        20,
        25,
        30,
        40,
        50,
        75,
        100,
      )),
      '#description' => t('Maximum number of currently online users to display.'),
    );
    return $form;
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockSubmit().
   */
  public function blockSubmit($form, &$form_state) {
    $this->configuration['seconds_online'] = $form_state['values']['user_block_seconds_online'];
    $this->configuration['max_list_count'] = $form_state['values']['user_block_max_list_count'];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockBase::blockValidate public function Adds block type-specific validation for the block form.
BlockBase::form public function Implements \Drupal\block\BlockPluginInterface::form(). Overrides BlockPluginInterface::form 1
BlockBase::getConfig public function Returns the configuration data for the block plugin.
BlockBase::setConfig public function Sets a particular value in the block settings.
BlockBase::submit public function Implements \Drupal\block\BlockPluginInterface::submit(). Overrides BlockPluginInterface::submit
BlockBase::validate public function Implements \Drupal\block\BlockPluginInterface::validate(). Overrides BlockPluginInterface::validate
BlockBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
UserOnlineBlock::access public function Overrides \Drupal\block\BlockBase::access(). Overrides BlockBase::access
UserOnlineBlock::blockForm public function Overrides \Drupal\block\BlockBase::blockForm(). Overrides BlockBase::blockForm
UserOnlineBlock::blockSubmit public function Overrides \Drupal\block\BlockBase::blockSubmit(). Overrides BlockBase::blockSubmit
UserOnlineBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
UserOnlineBlock::settings public function Overrides \Drupal\block\BlockBase::settings(). Overrides BlockBase::settings