You are here

function template_preprocess_search_api_page_results in Search API Pages 7

Preprocess variables for search-api-page-results.tpl.php.

Parameters

array $variables: An associative array containing:

  • index: The index this search was executed on.
  • results: An array of search results, as returned by SearchApiQueryInterface::execute().
  • view_mode: The view mode to use for results. Either one of the searched entity's view modes (if applicable), or "search_api_page_result" to use the module's builtin theme function to render results.
  • keys: The keywords of the executed search.
  • page: The search page whose search results are being displayed.

See also

search-api-page-results.tpl.php

File

./search_api_page.pages.inc, line 274
User page callbacks for the Search pages module.

Code

function template_preprocess_search_api_page_results(array &$variables) {
  $page = $variables['page'];
  $results = $variables['results'];
  $results += array(
    'results' => array(),
  );
  $variables += array(
    'items' => array(),
  );
  if ($results['results'] && empty($variables['items'])) {
    $variables['items'] = $variables['index']
      ->loadItems(array_keys($results['results']));
  }
  $variables['result_count'] = $results['result count'];
  $variables['sec'] = 0;
  if (isset($results['performance']['complete'])) {
    $variables['sec'] = round($results['performance']['complete'], 3);
  }
  $variables['search_performance'] = array(
    '#theme' => "search_api_page_search_performance__{$page->machine_name}",
    '#markup' => format_plural($results['result count'], 'The search found 1 result in @sec seconds.', 'The search found @count results in @sec seconds.', array(
      '@sec' => $variables['sec'],
    )),
    '#prefix' => '<p class="search-performance">',
    '#suffix' => '</p>',
  );

  // Make the help text depend on the parse mode used.
  switch ($page->options['mode']) {
    case 'direct':
      if ($variables['index']
        ->server()->class == 'search_api_solr') {
        $variables['no_results_help'] = t('<ul>
<li>Check if your spelling is correct.</li>
<li>Remove quotes around phrases to search for each word individually. <em>bike shed</em> will often show more results than <em>&quot;bike shed&quot;</em>.</li>
<li>Consider loosening your query with <em>OR</em>. <em>bike OR shed</em> will often show more results than <em>bike shed</em>.</li>
</ul>');
      }
      else {
        $variables['no_results_help'] = t('<ul>
<li>Check if your spelling is correct.</li>
<li>Use fewer keywords to increase the number of results.</li>
</ul>');
      }
      break;
    case 'single':
      $variables['no_results_help'] = t('<ul>
<li>Check if your spelling is correct.</li>
<li>Use fewer keywords to increase the number of results.</li>
</ul>');
      break;
    case 'terms':
      $variables['no_results_help'] = t('<ul>
<li>Check if your spelling is correct.</li>
<li>Remove quotes around phrases to search for each word individually. <em>bike shed</em> will often show more results than <em>&quot;bike shed&quot;</em>.</li>
<li>Use fewer keywords to increase the number of results.</li>
</ul>');
      break;
  }

  // Prepare CSS classes.
  $classes_array = array(
    'search-api-page',
    'search-api-page-' . drupal_clean_css_identifier($page->machine_name),
    'view-mode-' . drupal_clean_css_identifier($variables['view_mode']),
  );
  $variables['classes'] = implode(' ', $classes_array);

  // Distinguish between the native search_api_page_result "view mode" and
  // proper entity view modes (e.g., Teaser, Full content, RSS and so forth).
  $variables['search_results'] = array();
  if ($variables['view_mode'] != 'search_api_page_result') {

    // Check if we have any entities to show and, if yes, show them.
    if (!empty($variables['items'])) {
      $variables['search_results'] = entity_view($variables['index']
        ->getEntityType(), $variables['items'], $variables['view_mode']);
    }
  }
  else {
    foreach ($results['results'] as $item) {
      if (!empty($variables['items'][$item['id']])) {
        $variables['search_results'][] = array(
          '#theme' => "search_api_page_result__{$page->machine_name}",
          '#index' => $variables['index'],
          '#result' => $item,
          '#item' => $variables['items'][$item['id']],
        );
      }
    }
  }

  // Load CSS.
  $base_path = drupal_get_path('module', 'search_api_page') . '/';
  drupal_add_css($base_path . 'search_api_page.css');
}