views:

445

answers:

4

Hi friends,

I need to display some specific cck fields such as Telephone Number, Address, etc. at search result page. As I see the search-result.tpl.php, there is no $node->... So how can I get the node fields into search result page?

Thanks a lot! Appreciate!


[SORTED]

wow, that simple :)

search-result.tpl.php

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

you could use the template_preprocess_search_result(&$variables) function (copy it from the search module and place it in your theme's template.php file) - and use the $variables variable (var_dump it) to retrieve your node info and print it

30equals
+1  A: 

In your theme, you can use template_preprocess_search_result()

function YOURTHEME_preprocess_search_result(&$variables) {
  if ($variables['type'] == 'node') {
    $node =& $variables['result']['node'];
    //Do something here
  }
}

Or, you can also implement hook_nodeapi() in a custom module for $op = 'search result'. The result of hook_nodeapi($node, 'search result') is available in $info_split in the search-result.tpl.php template.

mongolito404
A: 

We had to create a custom search once. We created a custom module that allowed not only writing a custom query to the database but also choose the kind of amount that gets displayed. We hooked into hook_search with the module. To give you an idea:

function yourmodule_search($op = 'search', $keys = NULL) {

switch ($op) {
   //some code, basically a stripped version from what the API shows us
    case 'search':

      $find = do_event_search($keys); //the actual search

      // Load results.
      $results = array();

      foreach ($find as $item) { 

        //iteration and adding certain elements to $resuls needned by Drupal
      }
      return $results;
  }
}

The interessting part for you is the retrival of the search results as done by do_event_search:

//all data comes from a queries to the database
$search_result[] = array ('link' => trim($link),
                    'snippet' => trim_text(i18n_get_lang() == "en" ? $row->description_en : $row->description_de),
                    'head' => trim_text(i18n_get_lang() == "en" ? $row->title_en : $row->title_de),
                    'type' => $type
                );

As you can see, snippet holds the text that should be displayed on the search pages and we select a certain $row from a custom database. The API example shows all fields that are needed for displaying the results properly.

DrColossos
A: 

[SORTED]

wow, that simple :)

search-result.tpl.php

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