How do you configure your search results to display an image thumbnail?
I have a gallery of over 10,000 images, and I need thumbnails in my search results.

I've found some information pertaining to the theme_search_item function in the search.module file, but I guess I am too dumb to understand how to implement it.

This question has been asked a bunch of times on the support forums, but I couldn't find a post that had any replies.

I really need help with this! I need this live before the site launches.
Thanks in advance,
Terry

I am using Drupal 5.9 and Image module 5.x-1.6 if that helps.

Comments

WorldFallz’s picture

What you're trying to do is known as "Overriding Theme Functions" in drupal-speak. Did you find Customize the search results page in your searches?

You need to copy the theme_search_item function from the search.module file into the appropriate theme file (if you're using a phptemplate based theme, it would be the template.php file in your theme's directory), rename it to phptemplate_search_item (assuming you're using a phptemplate based theme, if not see the link above), and then alter it as you wish. The $output you return will be your themed search item. For example, if you simply put

function phptemplate_search_item($item, $type) {
  $output = "<b>Hello World</b>";
  return $output:
}

All your search items should appear like: Hello World (though I've never tried this, lol).

If you're not sure what info you have available to you to use in forming your $output you can add the following to your function (just remember to remove it when you're done):

<?php
print_r ('<pre>' . $item . '</pre>');
print_r ('<pre>' . $type . '</pre>');
?>

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

terryit3’s picture

I have tried the advice you have given me, but I still can't get images or thumbnails to appear.

I added the:

<?php
print_r ('<pre>' . $item . '</pre>');
print_r ('<pre>' . $type . '</pre>');
?>

and it returned ' . $node . ' instead of the node title and information.

It may be asking too much, but could you please post some code that I could possibly use to include the image?

I thought that I had found what I needed on this thread, http://drupal.org/node/86987 but it was a dead end as well.

Thanks again!

WorldFallz’s picture

I'm not using the image module so this is a complete guess, but try putting the following in your template.php:

function phptemplate_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['type'] == 'image') {
    $info[] = image_display($item['node'], 'thumbnail');
  }
  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;
}

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

terryit3’s picture

Tried it, but nothing has changed :(

I'm using the NewsFlash theme from RoopleTheme, so it is a PHPTEMPLATE compatible theme.

WorldFallz’s picture

I played with it some, and I can confirm that the following works for imagefield images (with imagecache providing the thumbnails):

<?php
function phptemplate_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']);
  }
  if ($item['type'] == 'Image') {
    $image_filepath = $item['node']->field_image[0]['filepath'];
    $info[] = '<br />' . theme('imagecache', 'thumbnail', $image_filepath);
  }
  $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'<p class="search-info">'. implode(' - ', $info) .'</p></dd>';

  return $output;
}
?>

You can try the following:

<?php
function phptemplate_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']);
  }
  if ($item['type'] == 'Image') {
    $image_filepath = $item['node']->field_image[0]['filepath'];
    $info[] = '<br />' . image_display($item['node'], 'thumbnail');
  }
  $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'<p class="search-info">'. implode(' - ', $info) .'</p></dd>';

  return $output;
}
?>

As i said, i don't use the image module, so I have no clue if that will work. If it doesnt, you'll have to play with the "if ($item['type'] == 'Image') {" block to get it to work with the image module (and 'Image' should be the content type name of your image content type).

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

terryit3’s picture

You are awesome!

The code worked and images are now being displayed in my search results.

Thanks again,
Terry

WorldFallz’s picture

wow-- that was pure luck, lol. I always have to have a live test site to get the code correct. glad it worked for ya... and you're welcome ;-)

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

daniel wentsch’s picture

Hey Worldfallz,

thanks for your support.. not the first time I appreciate your efforts here, so thanks for the time you invest here :)

The posted code is just what I needed to get futher on my project!

WorldFallz’s picture

Happy to help and thanks for noticing and commenting back. ;-)

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

waynedrupal’s picture

subscribed.

fehin’s picture

subscribing

doktorrr’s picture

Can U help me with my site? This code not work for me. I've using Fusion theme.
My site is http://e-oglasi.biz