0

As an example, the hook_search_execute function returns the following array:

$results[] = array(
      'link' => url('node/' . $item->sid, array('absolute' => TRUE)),
      'type' => check_plain(node_type_get_name($node)),
      'title' => $node->title,
      'user' => theme('username', array('account' => $node)),
      'date' => $node->changed,
      'node' => $node,
      'extra' => $extra,
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, $node->body),
    );

'link' and 'title' are required and the others are optional. However, what if I wanted to add my element to the array like 'photo' and then have that available in the $search-result.tpl.php file?

Whe I add that element to the search results array, I indeed see it when I do a dump of $variables in _preprocess_search_result(). If it's available there, then how to I make it available as <?php print $photo; ?> in search-result.tpl.php?

1
  • If a variable is added in the preprocess function, it should automatically be available in the corresponding template. Jun 22, 2013 at 4:41

1 Answer 1

3

According to template_preprocess_search_result you have the $result variable available, so if you've already injected your custom field into the result you can just use

print $result['photo'];

In the template file.

Or if you're absolutely set on just having a $photo var, you need to add it to the variables for the template:

function MYMODULE_preprocess_search_result(&$vars) {
  $vars['photo'] = $vars['result']['photo'];
}
1
  • 1
    so $title, $snippet, etc are predefined somewhere in the system, and elements from that array are not assigned dynamically? I was hoping not to make the assignment twice, but if this is the way to go for extra $variables we'd like to show up in the existing template files, I'm happy with that. Thanks!
    – blue928
    Jun 23, 2013 at 2:54

Your Answer

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

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