39

I have customized the teaser view of my content types and the page view. Now I need to use the teaser view for the search results page. By default, Drupal displays a full text jumbled mess for each result, I need to be able to customize the look of each one per content type. I don't want to change HOW drupal gets the results, just how it displays them.

28

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.

  • Is there a way to theme search results using views or panels? – jan Dec 31 '12 at 15:35
  • @jan You can replace the search page with a view using exposed filters. – kiamlaluno Dec 31 '12 at 15:40
  • @kiamlaluno, I was hoping I can theme the default search option for drupal using views. Because I won't be using the exposed filters and just use the default search option. – jan Dec 31 '12 at 17:39
  • @jan If you use a view, the entire search page is replaced by the view. That is why you need to expose at least a view filter: It's what the users will use to enter the search terms. A view is just a list of nodes, and filters are used to filter the nodes that will be shown in the page. – kiamlaluno Dec 31 '12 at 17:54
  • @kiamlaluno you're right. Why I did not think about it!! Thanks – jan Jan 1 '13 at 12:19
18

In Drupal 7 you use ...

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

And you use ...

... 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>
  • 1
    Best explanation on this thread. – digitgopher Feb 24 '15 at 22:26
9

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.

  • 1
    I know this answer is from 2012, but I notice that on the module page it says search functionality is now excluded in Drupal 8. Which suggests this might not be the best thing to adopt for Drupal 7, assuming you want some kind of future proofing. That's not tested by myself though, just a heads up. – ja_him Apr 12 '16 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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