Changing the Drupal Search Results Page

drupal-search.jpg

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!

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)

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

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!