Drupal - How do I customize the search result page?

You can override theme_search_results(), or theme_search_result().

The first is the theme function that is invoked for the search result page, while the second is the theme function that is invoked to render a single result. Both the theme functions use a template file (search-results.tpl.php in the first case, search-result.tpl.php in the second case).

search-result.tpl.php would use $info_split['type'] in Drupal 6, which contains the node type. In the latest Drupal 7 version, $info_split['type'] is not anymore passed, but search-result.tpl.php still gets $result['node'], in the case the search is done for nodes; $result['node']->type is then the content type of the node.

To notice that Drupal 7, and higher, allows the modules to implement hook_search_page(), but it is a hook used by a module that integrates with the Search module, not from a module that wants to change the result page for results returned by another module.
Also, on Drupal 7, each theme function uses a preprocess function, which in this case would be hook_preprocess_search_results() and hook_preprocess_search_result(). They are helpful in the case you just need to edit the value of what passed to the template file.


In Drupal 7 you use ...

  • template_preprocess_search_results and
  • template_preprocess_search_result

... to tinker with the information displayed in the results.

And you use ...

  • search-results.tpl.php and
  • search-result.tpl.php

... to customize the markup of the results.

Example

This is an example of how to use the content type's teaser as search result. The next snippet goes in the theme's template.php

/**
 * Implements template_preprocess_search_result
 * @param type $vars
 */
function MYTHEME_preprocess_search_result(&$vars) {
  $node = $vars['result']['node'];
  if ($node->nid) { // if the result is a node we can load the teaser
    $vars['teaser'] = node_view($node, 'teaser'); 
  }
}

This snippet is the search-result.tpl.php file:

<article>
  <?php if ($teaser) : // for nodes we can use the teaser as search result ?>
    <?php print drupal_render($teaser); ?>
  <?php else : // for other results we use the default from core search module ?>  
    <?php print render($title_prefix); ?>
    <h3><a href="<?php print $url; ?>"><?php print $title; ?></a></h3>
    <?php print render($title_suffix); ?>
    <?php if ($snippet) : ?>
      <p><?php print $snippet; ?></p>
    <?php endif; ?>
  <?php endif; ?>
  <?php if ($info): ?>
    <footer><?php print $info; ?></footer>
  <?php endif; ?>
</article>

I recently spent a good deal of time working with search results on a Drupal 7 architecture website, and settled on using the Display Suite module.

The Display Suite module has a wonderful method for taking control of search results: It will allow you to easily use your teasers in search results. This is a tutorial by the maintainer of the module that focuses on using it for search results.

As always with Drupal, there are a variety of ways to do the same thing. I liked this method because it allowed me to break out the results by content type.

Tags:

Search

Nodes

7