0

I have a Resource content type that has an image field (field type and widget are both 'image'); the standard image type. The title of this field is 'Icon' and the machine name is 'field_icon'. I created my teaser so that the title, date, image, and description fields show and customized my search-result.tpl.php and template.php files so they are displaying the teaser/snippet. That works fine.

However, now I'd like to be able to format the returned results a bit more. I see how I can put HTML directly in the .tpl file, fine. But I want to customize the image field so it appears to the LEFT (float or align left) of the other returned teaser elements.

My problem is I can't figure out how to get the variable that represents the image so I can manipulate it in my .tpl file. I tried the following, but it just returns the number to indicate there IS an image or not:

$result['node']->'field-icon';

Can someone please tell me how to get the proper variable or link to the icon itself so I can manipulate it in my .tpl file?

Thank you.

Note, here's my current search-result.tpl.php code:

<li class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php print render($title_suffix); ?>
  <div class="search-snippet-info">
    <?php if ($snippet): ?>
      <?php print $snippet; ?>
    <?php endif; ?>
  </div>
</li>

1 Answer 1

4

Perhaps something like this will help:

/**
 * Implements hook_preprocess_search_result()
 */
function MYTHEME_preprocess_search_result(&$variables) {    
    $node = $variables['result']['node'];
    if (is_object($node) && $node->nid) { // if the result is a node we can load the teaser
        $variables['teaser'] = node_view($node, 'teaser');
    }
}

That will provide the variable $teaser to your search template. In search-result.tpl.php you can then access your field data:

<div class="search-snippet-info">
    <?php print render($teaser['field_icon']); ?>
    <h2><a href="<?php print $url; ?>"><?php print $title; ?></a></h2>
    <?php if ($snippet): ?>
      <?php print $snippet; ?>
    <?php endif; ?>
</div>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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