Drupal 7: To show the thumbnail image in search result page in Drupal 7

If you want Drupal 7 to show the thumbnail image in search result page please do take the following steps

Step 1. Add the below code into your theme’s template.php file


function THEMENAME_preprocess_search_result(&$vars, $hook) {
$vars['node'] = $vars['result']['node'];
}

Step 2. Modify search-result.tpl.php to display the thumbnail image of the orginal image,

find the ‘search-result.tpl.php’ file(located in core’s modules/search folder), make a copy named exactly the same inside your theme’s folder and add the below line of code wherever you’d like the picture to show up (Please note that the imagefield name is ‘field_product_picture’).

 

<?php
/* image start */
// product
$node = node_load($node->entity_id);
if (file_exists($node->field_product_picture['und'][0]['uri'])) {
// theme $title as image with theme_image()
$product_img = theme('image_style', array('style_name' => 'thumbnail', 'path' => $node->field_product_picture['und'][0]['uri'], 'getsize' => TRUE, 'attributes' => array('class' => 'thumb')));
print '<span>' . $product_img . '</span>';
}
?>

This entry was posted in Drupal Developer Notes. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *