You are here

function commons_search_solr_user_apachesolr_search_page_alter in Drupal Commons 7.3

Implements hook_apachesolr_search_page_alter(). We override the default search page for people here when no results are entered. This gives us a default people listing which we can filter to who we are following.

File

modules/commons/commons_search/modules/commons_search_solr_user/commons_search_solr_user.module, line 72

Code

function commons_search_solr_user_apachesolr_search_page_alter(&$build, $search_page) {
  global $user;
  if ($search_page['page_id'] == 'user_search' && isset($build['search_results']['#markup']) && empty($build['search_form']['basic']['keys']['#default_value'])) {

    // If the #markup item is set, it usually means we have no Solr results.
    // So lets just fetch all users that are active, and display those.
    // First check to see if the flagged key is set. If so we don't want to query
    // Using EFQ because flags aren't entities.
    $limit = isset($search_page['settings']['apachesolr_search_per_page']) ? $search_page['settings']['apachesolr_search_per_page'] : 12;
    $flagged = isset($_GET['flagged']) && is_numeric($_GET['flagged']) ? check_plain($_GET['flagged']) : 0;
    $out_results = array();
    if ($flagged && ($flag = flag_get_flag('commons_follow_user'))) {

      // Get the count of flagged users for a particular account.
      $fl_results = db_select('flagging', 'f')
        ->fields('f', array(
        'entity_id',
        'uid',
      ))
        ->condition('f.entity_type', 'user', '=')
        ->condition('f.uid', $user->uid, '=')
        ->condition('f.fid', $flag->fid, '=')
        ->extend('PagerDefault')
        ->limit($limit)
        ->addTag('commons_search_solr_user_people')
        ->addMetaData('flagged', $flagged)
        ->execute();
      while ($record = $fl_results
        ->fetchAssoc()) {
        $out_results[] = user_view(user_load($record['entity_id']), 'search_results');
      }
      $build['pager'] = array(
        '#theme' => 'pager',
      );
    }
    else {
      $users = new EntityFieldQuery();
      $results = $users
        ->entityCondition('entity_type', 'user')
        ->propertyCondition('status', 1)
        ->addTag('commons_search_solr_user_people')
        ->pager($limit, 1)
        ->execute();
      $user_ids = array_keys($results['user']);
      foreach ($user_ids as $uid) {
        $out_results[] = user_view(user_load($uid), 'search_results');
        $user_ids;
      }
      $build['pager'] = array(
        // As nice as it would be to make this use #theme, it is a bit more
        // trouble than it is worth to process $user->pager to conform to
        // renderable array standards. A custom theme suggestion should work
        // for most, if not all, situations for theme overrides.
        '#markup' => theme('pager__commons_search_solr_user', $users->pager),
      );
    }

    // Build renderable arrays for display.
    if (!empty($out_results)) {
      $build['search_results'] = array(
        '#theme' => 'commons_search_solr_user_results',
        '#title' => t('People directory'),
        '#results' => $out_results,
      );
    }
  }
}