Friday, October 10, 2008

Theme Drupal Search Results

I had the most awful time trying to theme the drupal search results page and remove the silly "node type", "author", "date" and "extra" info from the search results.

Initially I tried the easy route which was to copy modules/search/search-result.tpl.php into my theme and then in the themes template.php file remove the search-info div. Because that's the info I didn't want to show up.

Didn't work. Worked only for /search/node. Failed when going to /search/node/some%20search%20term.

Finally was able to get it working by implementing hook_prepocess_search_result, and making sure the $info array didn't have anything.

* Implement hook_preprocess_search_result()
function phptemplate_preprocess_search_result(&$variables) {
$result = $variables['result'];
$variables['url'] = check_url($result['link']);
$variables['title'] = check_plain($result['title']);

$info = array();
if (!empty($result['type'])) {
$info['type'] = check_plain($result['type']);
if (!empty($result['user'])) {
$info['user'] = $result['user'];
if (!empty($result['date'])) {
$info['date'] = format_date($result['date'], 'small');
if (isset($result['extra']) && is_array($result['extra'])) {
$info = array_merge($info, $result['extra']);
// Check for existence. User search does not include snippets.
$variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
// Provide separated and grouped meta information..
$variables['info_split'] = $info;
$variables['info'] = implode(' - ', $info);
// Provide alternate search result template.
$variables['template_files'][] = 'search-result-'. $variables['type'];

With this gem in my hook_theme implementation

'search_result' => array(
'arguments' => array('result' => NULL, 'type' => NULL),
'template' => 'search-result',
'path' => drupal_get_path('module', 'search')


vince said...

Could you please explain how to implement this? I don't understand in which files i have to add this code to make it work... Thanks!

Idaho SEO said...

I had to put those functions in the template.php file for my active theme.


vince said...

Sorry to bother you again, but i still cannot get it to work...
I have put the code in my template.php, but i think the bottom part after "With this gem in my hook_theme implementation" needs to go somewhere else. I have tried several things, but the search results still look the same. Where should the bottom part go exactly?

vince said...

duh... i figured it out myself:

i already had a declaration of MYTHEME_theme and added the code over there (already tried that)

THEN i also had to clear my cache!!

Thanks again for your blogpost!

koobmeej said...

Your implementation of hook_theme needs to be in the template.php file. Remember to replace hook_ with yourthemename_. You may also need to go to /admin/build/modules so that your theme gets reloaded and /admin/settings/performance to clear the cache.

streche said...

Thanks for the post. I finally got it working.

My theme didn't have hook_theme implemented so I had to add it.

function YourTheme_theme() {
return array(
'search_result' => array(
'arguments' => array('result' => NULL, 'type' => NULL),
'template' => 'search-result',
'path' => drupal_get_path('module', 'search')

handsofaten said...

i already have a hook_theme that looks like this:

function zen_classic_theme(&$existing, $type, $theme, $path) {
return zen_theme($existing, $type, $theme, $path);

does anyone know where i would put the array at the end of the post?

koobmeej said...

zen_theme returns an array. you could try extending that array with the search_result info.

$x = zen_theme(...);
$x['search_result'] = array(...);
return $x;

replacing the ... with all the stuff i'm to lazy to write.

araon said...

Really great ideas. I like every example. Just might have to try these...
more template

Drupal Design Inspiration said...

Thanks for posting this. Seems like authoring info should be optional for search results.