You are here

public function GoogleCSESearch::buildResults in Google Custom Search Engine 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Search/GoogleCSESearch.php \Drupal\google_cse\Plugin\Search\GoogleCSESearch::buildResults()

Executes the search and builds render arrays for the result items.

Return value

array An array of render arrays of search result items (generally each item has '#theme' set to 'search_result'), or an empty array if there are no results.

Overrides SearchPluginBase::buildResults

File

src/Plugin/Search/GoogleCSESearch.php, line 191

Class

GoogleCSESearch
Handles searching for node entities using the Search module index.

Namespace

Drupal\google_cse\Plugin\Search

Code

public function buildResults() {
  $results = $this
    ->execute();

  // @see https://www.drupal.org/node/2195739
  if (!$this->configuration['use_adv']) {
    $output[] = [
      '#theme' => 'google_cse_results',
    ];
    return $output;
  }
  if (!$results) {

    // No results found.
    $output[] = [
      '#theme' => 'google_cse_search_noresults',
    ];
  }
  if ($this->requestStack
    ->getCurrentRequest()->query
    ->has('page')) {
    $current_page = $this->requestStack
      ->getCurrentRequest()->query
      ->get('page');
    $number_results = t('Results @from to @to of @total matches.', array(
      '@from' => $current_page * 10,
      '@to' => $current_page * 10 + 10,
      '@total' => $GLOBALS['pager_total_items'][0],
    ));
    $output['prefix']['#markup'] = $number_results . '<ol class="search-results">';
  }
  foreach ($results as $entry) {
    $output[] = [
      '#theme' => 'search_result',
      '#result' => $entry,
      '#plugin_id' => $this
        ->getPluginId(),
    ];
  }
  if ($this->requestStack
    ->getCurrentRequest()->query
    ->has('page')) {

    // Important, add the pager.
    $pager = [
      '#type' => 'pager',
    ];
    $output['suffix']['#markup'] = '</ol>' . $this->renderer
      ->render($pager);
  }
  return $output;
}