tags:

views:

219

answers:

3

Hi!

How to display custom CCK field (text or imagefield) in Drupal core search results page?

Thank you in advance!

A: 

It depends how you do the search.

If you use views to build a search, you can decide yourself what to show.

If you're using some other search mechanics, you can use a combination of a proprocess hook, theming function, template you get the output you want. You should have the node object available, so displaying a CCK should be easy enough.

Edit:
For the Drupal core search module you need to overwrite the search-result.tpl.php in your theme, to alter how search results are printed. Here you can add or delete info. If you need more variables, you can create them for use in the template in a process hook. This is basic Drupal theming, check out the handbook.

googletorp
Thank you! There is no problem with Views, but can't find solution for Drupal core search module. May be you have an example?
romandor
A: 

You need to override search-result-tpl.php in your theme. Copy it from modules/search to your themes directory, clear the theme cache, and you're set. You'll see that there is an array available to the theme file called 'result', which contains a bunch of data, including a node object. So your file becomes something like:

<dt class="title">
  <a href="<?php print $url; ?>"><?php print $title; ?></a>
</dt>
<dd>
<?php
 // Here is the change
 print $result['node']->field_name_of_cck_field['view'];
 ?>
  <?php if ($snippet) : ?>
    <p class="search-snippet"><?php print $snippet; ?></p>
  <?php endif; ?>
  <?php if ($info) : ?>
  <p class="search-info"><?php print $info; ?></p>
  <?php endif; ?>
</dd>

Good luck!

cam8001
It may just be $node-> rather than $result['node']. I don't have an install handy to check.
cam8001
['view'] don't work, I use ['value'] instead.
romandor
Value is a raw user input field, so potentially it may contain unsafe data. View is processed and guaranteed safe.
cam8001
A: 

1 Copy search-result.tpl.php file from modules/search to your theme folder

2 For CCK text field add:

<?php if ($result['node']->field_name[0]['value']): ?>
  <h4><?php print($result['node']->field_name[0]['value']); ?></h4>
<?php endif; ?>

3 For imagefield with imagecache:

<?php if ($result['node']->field_logo[0]['filename']): ?>
  <img src="/sites/default/files/imagecache/path_to_file/<?php print $result['node']->field_logo[0]['filename']; ?>" />
<?php endif; ?>

4 CSS styling next.

Thanx for cam8001 & googletorp!

romandor