1

I am using the drupal's built in search module, for this I have copied the search-results.tpl.php in my theme folder. Now I have to add images for search results too and also want to custom theme the template file. For images I have used the following approach: In my template.php:

function MyTheme_preprocess_search_result(&$variables)
{
if ($result['node']->type == "content_type") {
 $node = node_load($result['node']->nid);
 $prod_image = field_view_field('node', $node, 'machine_name_img', $display = array());
 $variables['image'] = $prod_image;
}
}

and then in search-result.tpl.php:

<?php if($prod_image):?>
    <?php print render($prod_image);?>
<?php endif; ?>

but its not working, can anybody knows how do I get the images related to search results, and how can I theme search results?

0

You're using the wrong var name. In your preprocess function you're using $variables['image'] = $prod_image; to set the var. Whatever element name you use in the the $variables array will be the name of the var in the template. So in your case it should be:

<?php if($image):?>
    <?php print render($image);?>
<?php endif; ?>
  • Thanku @Shawn, but still not working – nomeer May 18 '15 at 5:59
  • Yeah its working now, in ` hook_preprocess_search_result(&$variables)` I have used field_get_items('node',$node, 'machine_name_img') before field_view_field('node', $node, 'machine_name_img', $display = array()) – nomeer May 18 '15 at 10:03
0

You used some vars that don't even exist in this place. template.php:

function MyTheme_preprocess_search_result(&$variables){
  if ($variables['result']['type'] == "content_type") {
   $node = node_load($variables['result']['node']->entity_id);
   $prod_image = field_view_field('node', $node, 'machine_name_img', $display = array());
   $variables['image'] = $prod_image;
  }
}

and then in search-result.tpl.php(like in the answer of @Shawn Conn):

<?php if(image): ?>
  <?php print render(image); ?>
<?php endif; ?>

If you get the correct return out of this, dependes on what the line $prod_image = field_view_field('node', $node, 'machine_name_img', $display = array()); will return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.