function user_block_view

Implements hook_block_view().

1 call to user_block_view()
UserBlocksTests::testWhosOnlineBlock in drupal/core/modules/user/lib/Drupal/user/Tests/UserBlocksTests.php
Test the Who's Online block.

File

drupal/core/modules/user/user.module, line 718
Enables the user registration and login system.

Code

function user_block_view($delta = '') {
  global $user;
  $block = array();
  $block_config = config('user.block');
  switch ($delta) {
    case 'login':

      // For usability's sake, avoid showing two login forms on one page.
      if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {

        // Customize the login form.
        $form = drupal_get_form('user_login_form');
        unset($form['name']['#attributes']['autofocus']);
        unset($form['name']['#description']);
        unset($form['pass']['#description']);
        $form['name']['#size'] = 15;
        $form['pass']['#size'] = 15;
        $form['#action'] = url(current_path(), array(
          'query' => drupal_get_destination(),
          'external' => FALSE,
        ));

        // Build action links.
        $items = array();
        if (config('user.settings')
          ->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
          $items['create_account'] = l(t('Create new account'), 'user/register', array(
            'attributes' => array(
              'title' => t('Create a new user account.'),
              'class' => array(
                'create-account-link',
              ),
            ),
          ));
        }
        $items['request_password'] = l(t('Request new password'), 'user/password', array(
          'attributes' => array(
            'title' => t('Request new password via e-mail.'),
            'class' => array(
              'request-password-link',
            ),
          ),
        ));

        // Build a block as renderable array.
        $block['subject'] = t('User login');
        $block['content'] = array(
          'user_login_form' => $form,
          'user_links' => array(
            '#theme' => 'item_list',
            '#items' => $items,
          ),
        );
      }
      return $block;
    case 'new':
      if (user_access('access content')) {

        // Retrieve a list of new users who have subsequently accessed the site successfully.
        $from = 0;
        $count = $block_config
          ->get('whois_new_count');
        $items = db_query_range('SELECT uid, name FROM {users} WHERE status <> 0 AND access <> 0 ORDER BY created DESC', $from, $count)
          ->fetchAll();
        $block['subject'] = t('Who\'s new');
        $block['content'] = array(
          '#theme' => 'item_list__user__new',
          '#items' => array(),
        );
        foreach ($items as $account) {
          $block['content']['#items'][] = theme('username', array(
            'account' => $account,
          ));
        }
      }
      return $block;

    // @todo: Move this block to statistics.module and remove dependency on
    //   user.access.
    case 'online':
      if (user_access('access content')) {

        // Count users active within the defined period.
        $interval = REQUEST_TIME - $block_config
          ->get('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();
        $block['subject'] = t('Who\'s online');
        $block['content'] = array(
          '#theme' => 'item_list__user__online',
          '#items' => array(),
          '#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 = $block_config
          ->get('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) {
            $block['content']['#items'][] = theme('username', array(
              'account' => $account,
            ));
          }
        }
      }
      return $block;
  }
}