You are here

public function ResultSet::getPager in Google Search Appliance 8

Returns render array for pager.

Return value

array Render array.

File

src/SearchResults/ResultSet.php, line 426

Class

ResultSet
Defines a value object for a search response.

Namespace

Drupal\google_appliance\SearchResults

Code

public function getPager() {

  // Globals required to manually configure the pager.
  global $pager_page_array, $pager_total, $pager_total_items;
  $element = 0;

  /* @note
   * The total results count from the GSA is unreliable. The docs
   * state that it is an *approximation*, but if you click around on
   * enough searches, you'll find that it's off by a considerable amount -
   * enough to break the math used to create pager links. It's not all that
   * noticeable unless you click on the "last" link in the pager, and
   * notice that in some searches, the last page is paginated at a number
   * less than was previously viewable when the first page of results came
   * up on the initial search view.
   *
   * The problem is rooted in public vs. access-controlled indexing.
   * @see groups.google.com/group/Google-Search-Appliance-Help/browse_thread/thread/019b77fb3e7950c7
   * Access-controlled results are counted before we know if we can actually
   * view it. Device configuration can help.
   *
   * @TODO: perhaps a better solution is to just query the device for the
   * first X (up to 1000) results and cache them locally.
   */

  // Manually configure (fake) the pager.
  $page = $this->query
    ->getPage();

  // Convert page id to array.
  $pager_page_array = explode(',', $page);

  // Set the total results.
  $pager_total_items[$element] = (int) $this->total;

  // Set the total # of pages.
  $pager_total[$element] = ceil($pager_total_items[$element] / $this->resultsPerPage);
  $pager_page_array[$element] = max(0, min((int) $pager_page_array[$element], (int) $pager_total[$element] - 1));
  return [
    '#type' => 'pager',
  ];
}