Drupal – Add Image to Apache Solr Search Result

Apache Solr is a very good web search engine and it works very well with Drupal. This post shows you how to customize the default pure text search result template by adding node image to each result record.

First, we need to load the corresponding node for each search result. This can be done in the <theme>_preprocess_search_result(&$variables) function. Add it to the template.php.

function <theme>_preprocess_search_result(&$vars) {
  // Add node object to result, so we can display imagefield images in results.
  $n = node_load($vars['result']['node']->nid);
  $n && ($vars['node'] = $n);


Then we can customize the search-result.tpl.php. If you don’t have this file in theme folder, create a new one and copy the source @ Drupal API – search-result.tpl.php source

In the search-result.tpl.php, add the following code to get the node image for each record.

<p class="search-result-image"><?php print theme('imagecache', '<imagecache_preset>', $node->field_cck_image[0]['filepath']); ?></p>


Done =)

Reference: Midwestern Mac, LLC – Adding Images to Search Results (Drupal Search)

2 thoughts on “Drupal – Add Image to Apache Solr Search Result”

  1. I get the following error when I use this method:
    Notice: Undefined property: stdClass::$nid in lendit_preprocess_search_result()


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.