Changing the Drupal Search Results Page

Today I needed to customize the themed results of the Drupal search. I have a pretty good thought of the right way to do this and I am going to attempt to do this without accessing any documentation online about the process. That way I can explain the steps of how to do this, in the Drupal way, from my experience and perspective. I will also try and document my thought process in order possibly help others when trying to do similar Drupal customization.

The first step I am going to take is to find out what is driving the Drupal search. Most likely it is a module that is installed in core Drupal so lets go look in the core modules directory and take a look. So, in the modules directory I see the search folder which is a pretty good sign that this is what drives the Drupal search. Now I am going to open the search module folder to see what is inside and also to see what I can customize, keeping in mind that I will not actually be doing any changes to any files in the search module itself.

Inside the search module I find the standard .info, .install, .module and a .css file. I am most interested in looking at the search.module itself. So what am I looking for you may ask, well it is something I can hook into or theme. I use ActiveState Komodo so after opening search.module I select the code tab in my left plane. What this shows me is all the functions that are available from within the the search module and I see the functions I am looking for. theme_search_theme_form, theme_search_block_form, theme_search_item and theme_search_page. I am able to customize any one of these functions in a way that will not touch the actual Drupal core module. Great!

What I am most interested in is how to change the look of the search result page (or srp to all those seo gurus). All I need to do at this point is to remove the line of info that tells who wrote the node, when it was written, comments etc... From a guess, using how the theme functions are named, I decide that theme_search_page function is the one I will customize from within my own theme. And so you know, for this project I am using the zen theme that is located in my /sites/all/themes/zen folder. Here is the theme_search_page which is nicely commented so I can see the parameters that are being passed to this function.

/**
* Format the result page of a search query.
*
* Modules may implement hook_search_page() in order to override this default
* function to display search results. In that case it is expected they provide
* their own themeable functions.
*
* @param $results
*   All search result as returned by hook_search().
* @param $type
*   The type of item found, such as "user" or "node".
*
* @ingroup themeable
*/
function theme_search_page($results, $type) {
  $output = '<dl class="search-results">';
 
  foreach ($results as $entry) {
    $output .= theme('search_item', $entry, $type);
  }
  $output .= '</dl>';
  $output .= theme('pager', NULL, 10, 0);
 
  return $output;
}  



I notice that this function calls the theme_search_items function by way of theme('search_item', $entry, $type) so I now see that this is actually the function that I want to manipulate. This function looks like the following:

function theme_search_item($item, $type) {
  $output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
  $info = array();
  if ($item['type']) {
    $info[] = check_plain($item['type']);
  }
  if ($item['user']) {
    $info[] = $item['user'];
  }
  if ($item['date']) {
    $info[] = format_date($item['date'], 'small');
  }
  if (is_array($item['extra'])) {
    $info = array_merge($info, $item['extra']);
  }
  $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'<p class="search-info">'. implode(' - ', $info) .'</p></dd>';
  return $output;
}  



So what I do next is to copy all of the above function directly into my template.php file that is found in my /sites/all/themes/zen folder. The template.php file is what you can use in order to indirectly manipulate core Drupal. Not just for search, but for all core modules. I then change the name of the theme_search_item($item, $type) function to zen_search_item($item, $type). After saving the file, Drupal no longer calls search modules function but now calls my theme zen_search_item function. Simply awsome, and now when I have to do a core drupal update, my changes in the theme will not be over written when a core search module update is performed.

Don't forget to change the name of your template.php function or you may get the following error. "Fatal error: Cannot redeclare theme_search_item() (previously declared in....)" I just saying ... this didn't happen to me ;)

Now, lets get to there really good stuff. Because this new zen_search_item function is in my theme, I am pretty free to do whatever I want in order to get the results I desire. So, my quick change of removing the extra info that I mentioned previously can be accomplished by removing the code that contains the extra data that I don't need. My new function in my template.php file now becomes:

function zen_search_item($item, $type) {
  $output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
  $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '');
  return $output;
}  



So, I know you can see I removed a lot of info but at this point you can go both ways. You can add content, change the elements or do whatever you desire! Happy Drupaling!

Hi, Do you know how to use

Hi, Do you know how to use the search phrases for page title in search results in Drupal 6

Thanks!

Clean output

Hi! I know that this is a little bit old post but can you tell how can I have a clean html ouput for search result pages? Each time I try to include a special characters like "&" as part of the search term, I usually get results with "&" highlighted yet includes the HTML mark-up. Thus, the results has &,   " etc...

Thanks!

how do you hide/ remove

how do you hide/ remove (without using CSS) the search form (see: http://i53.tinypic.com/2mcajh5.png) in the search results page...

Re:

Really, why I love Drupal it's because it gives you good opportunities for creating a community site. Some of the modules that Wordpress and Joomla do not contain, are really much helpful. As far as this particular topic goes, here is a good video at http://www.tubesfan.com/watch/drupal-tutorial-theming-search-results explaining how we can theme Drupal core search module looking at how they may be improved and how such changes are implemented.

Drupal 6 Nothing works

I have tried all the method above and cannot get it to use any customizations to the search page in drupal 6! is there some kind of list of templates that I need to add the search page to? Or is it supposed to autodetect that I have included the search-result.tpl.php? Thanks!

Drupal 6 customization of the

For Drupal 6, instead of commenting out the search-info area, you can simply display the information you wish. Our client did not want to display the user field but wanted the type, date, and comment fields. So we replaced the info area with the following code:

<?php if ($info) : ?>

<?php if($info_split['type']) print $info_split['type'];
if((($info_split['type']) and ($info_split['date'])) || (($info_split['type']) and ($info_split['comment']))) echo " - ";
if($info_split['date']) print $info_split['date'];
if(($info_split['date']) and ($info_split['comment'])) echo " - ";
if($info_split['comment']) print $info_split['comment']; ?>
<?php endif; ?>

I have tried both the

I have tried both the way,
both working fine in drupal 6.
But can anybody tell me if i am using many database query in displaying search result
then which way is optimised? Doing code in the .tpl.php file or my theme's template file?
plz let me know asap.....

I work with D6, and my

I work with D6, and my fonction zen_search_item is not override!
I have zen_search_item in my phptemplate.php file, but it is already theme_search_item in my search module file wich process!

An idea ?

Thanks

This was very helpful.

doesnt work

this doesnt work for me either. but then i'm using Drupal 6 , rather than 5.

is there an updated version of this blogpost , for D6?

it's fairly easy...

1. Copy search-result.tpl.php from search module folder (like <drupal_root>/modules/search) to your theme folder (like <drupal_root>/sites/all/themes/<your_theme>)

2. Edit copied file as needed (I've commented out the "search-info" paragraph generation, for example)

3. Don't forget to clear cache before testing ("Administer" -> "Performance" -> "Clear cached data" button at the bottom)

Drupal 6 customize search results

Hello i confirm that copying search-result.tpl.php to the custom theme folder and editing it there works for Drupal 6.

Thank you kind Anonymous sir!

great explanation

great explanation of how to go about doing things like this. I'm new to drupal (2 months :-)) and am now at the stage where I want to do exactly this. Thanks!

not working for me

This is not working for me

just put the exact codes

just put the exact codes it'll work, i've never used drupal but i have tried now and it is working.

nice

This is a great article since it not only teaches a basic of Drupal theme over-rides, but also shows how your thought process should go in figuring out what to do next.

You may want to think about adding this info to the handbook on drupal.org if there isn't already one.

That's a good suggestion.

Yea, guess that is probably the best place for it. I didn't see a handbook page specifically for changing the results page so I added one at http://drupal.org/node/175013. Hey, that's my first handbook page!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
By submitting this form, you accept the Mollom privacy policy.