4

I just realized that template_preprocess_search_results() function has been left out in Drupal 8 for some reasons, although template_preprocess_search_result() (for single results) is still there.

I need to be able to manipulate the search results prior to rendering as I would with template_preprocess_search_results() in Drupal 7.

2 Answers 2

2

The method that builds the search results is SearchController::view(), which contains the following code.

  $results = array();
  if ($request->query->has('keys')) {
    if ($plugin->isSearchExecutable()) {
      // Log the search.
      if ($this->config('search.settings')->get('logging')) {
        $this->logger->notice('Searched %type for %keys.', array('%keys' => $keys, '%type' => $entity->label()));
      }

      // Collect the search results.
      $results = $plugin->buildResults();
    }
    else {
      // The search not being executable means that no keywords or other
      // conditions were entered.
      drupal_set_message($this->t('Please enter some keywords.'), 'error');
    }
  }

  if (count($results)) {
    $build['search_results_title'] = array(
      '#markup' => '<h2>' . $this->t('Search results') . '</h2>',
    );
  }

  $build['search_results'] = array(
    '#theme' => array('item_list__search_results__' . $plugin->getPluginId(), 'item_list__search_results'),
    '#items' => $results,
    '#empty' => array(
      '#markup' => '<h3>' . $this->t('Your search yielded no results.') . '</h3>',
    ),
    '#list_type' => 'ol',
    '#context' => array(
      'plugin' => $plugin->getPluginId(),
    ),
  );

So, you can use template_preprocess_item_list(&$variables), checking that $variables['#context']['plugin'] is set. (I am not sure about using template_preprocess_item_list__search_results(&$variables), since I have never tried using such preprocess function.)

The $plugin->buildResults() method called from SearchController::view() just builds the results as array of items themed with search_result.

  $results = $this->execute();

  $built = array();
  foreach ($results as $result) {
    $built[] = array(
      '#theme' => 'search_result',
      '#result' => $result,
      '#plugin_id' => $this->getPluginId(),
    );
  }
6
  • 2
    template_preprocess_item_list__search_results() should work now in Drupal 8 if you actually have such a template.
    – Berdir
    Commented Apr 29, 2016 at 20:19
  • That would make things easier, since the template would surely be used for the search result page.
    – avpaderno
    Commented Apr 30, 2016 at 6:55
  • @Berdir I did not find the function in the core docs so I just presumed it was removed. Can you please elaborate on the "if you have such a template"? Thanks!
    – Henkealg
    Commented May 2, 2016 at 5:59
  • @Henkealg Berdir was replying to my I am not sure about using template_preprocess_item_list__search_results(&$variables) since I have never tried using such preprocess function. He means you can use template_preprocess_item_list__search_results(&$variables) as long as you have a item_list__search_results.html.twig template.
    – avpaderno
    Commented May 2, 2016 at 6:58
  • Thanks for the follow-up. I was able to use template_preprocess_item_list__search_results() in the way you suggested even though it is not included in the documentation anymore (strange). Kiam; I will mark your answer as accepted to close this thread. Thanks again.
    – Henkealg
    Commented May 2, 2016 at 8:39
3

You can create a new module and inside your YOUR_MODULE.module file put this code:

function YOUR_MODULE_preprocess_item_list__search_results(&$variables) {
  //to use dpm() you nedd the devel module
  dpm($variables['items']);
}

Install your module and you will see the output in the search result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.