0

I'm using search.module. I used dpm(get_defined_vars() ); in my search-results.tpl.php file to see all returned variables, so I can reference a needed variable in some PHP code I'm writing in there.

However, although the variable is being returned in the teaser/snippet (and is outputting to the page) the only place I'm finding the 'variable' is in the $search_results array which is just a massively long string of html code. How can I parse out a needed variable (I assume in template.php) so that I can reference it and use it for comparisons on search-results.tpl.php?

The content is coming from a content type node called 'resource'. The field in questions is called 'field_type'. Here's the code I'm currently working with in template.php to get other variables:

function themename_preprocess_search_results(&$variables) {
        foreach($variables['results'] as $result) {
                $node = $result['node'];
                if($node->type == 'resource') {
                      $items = field_get_items('node', $node, 'field_type');
                      $variables['field_type_items'] = $items;
                      $variables['field_type_item'] = $items[0];
                      $field_view = field_view_field('node', $node, 'field_type');
                      $variables['field_type_rendered'] = render($field_view);
                }
        }
}

// function to enable correct variables for search results
function themename_preprocess_search_result(&$variables) {
  $variables['search_results'] = '';
  if (!empty($variables['module'])) {
    $variables['module'] = check_plain($variables['module']);
  }
  /*foreach ($variables['results'] as $result) {
    $variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
  }*/
  $teaser = node_view($variables['result']['node'], 'teaser');
  $variables['teaser'] = $teaser;
  /*
  $variables['body'] = substr(render($teaser['body']), 0, 200);
  if (strlen($variables['body']) >= 200) {
    $variables['body'] .= "...";
  }
  */
  $variables['snippet'] = drupal_render($teaser);
  $variables['info'] = '';
  $variables['pager'] = theme('pager', array('tags' => NULL));
  $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
}

Is there something I can add to this to get the field field_type and pass it to my tpl files?

Or, if that's not the right solution, what is?

1

You need to implement a preprocess hook for template_preprocess_search_results() in your theme's template.php file. The node object should be available in the variables array:

/**
 *  Implements hook_preprocess_HOOK()
 */
function themename_preprocess_search_results(&$variables) {
  foreach ($variables['results'] as $result) {
    $node = $result['node'];
    if ($node->type == 'resource') {
      // Use the field API to pass the field on to the template.
      $items = field_get_items('node', $node, 'field_type');

      // Pass this field item to the $variables array for use in the template.
      // If the field accepts multiple values
      $variables['field_type_items'] = $items;
      // Or, for single-value fields
      $variables['field_type_item'] = $items[0];

      // Or pass the rendered HTML for this field only:
      $field_view = field_view_field('node', $node, 'field_type');
      $variables['field_type_rendered'] = render($field_view);
    }    
  }
}

You don't need to copy in the code from template_preprocess_search_results(). That function prepares the $variables array before it is passed to your preprocess function. That's why $variables['search_results'] is already themed HTML.

  • Thanks for your help, see my comment above to Darvanen. I've got my search tpl getting the node variable now with the field_type and everything, but actually accessing and using that variable now that I've got it is turning out to be troublesome. Any additional help is appreciated. Thank you very much for all your help so far. – Sage Mar 12 '15 at 9:48
  • You should use the Field API to access field items, get their formatted output, etc. It is best to do any complex logic in the template.php file preprocess functions and then pass simple flags or data to the template in the variables array. I've updated the example with a simple example of passing a particular field's values to the template separately. – montaigne Mar 12 '15 at 15:03
  • I've updated my code above. I'm not getting any errors per se, but when I try and reference my field on my search page (search-results.tpl.php) using echo, dpm, or var_dump I get errors. I've tried $variables['field_type_rendered'], $field_type_rendered, etc. Also, dpm($variables) is showing no change no matter that I added all that extra code to template.php. The only value I get added to 'results' is the node object I mentioned before, no parsing out of that one variable is taking place... – Sage Mar 12 '15 at 20:13
  • nevermind, I got it working with $field_type_rendered. Must have been a caching issue or something. Restarting my computer helped (but drush cc all did not...). Odd that. Anyway, thanks again for your very nice help. – Sage Mar 12 '15 at 21:37
  • Glad you got it working. Note that the variables used in the template file (search-results.tpl.php) are taken from the keys of the $variables array in the theme preprocess functions. So $variables['field_type_rendered'] in the hook_preprocess_HOOK() function becomes $field_type_rendered in the tpl.php file. – montaigne Mar 12 '15 at 23:31
1

Adding fields programatically

Use plain old dpm($variables); in your search-results.tpl.php file to check variables.

And yes, the preprocess function is where to add in extra information, you can use something like:

$variables['custom'] = field_view_field($entity_type, $entity, $field_name)

field_view_field prevents you from having to load the whole node and takes care of languages etc for you.


Adding fields in the UI

Go to the Manage Display tab for the content type in question and scroll down to CUSTOM DISPLAY SETTINGS.

Turn on the Search result checkbox and save, then go to the Search Result sub-tab.

Arrange your fields how you wish them to show in search results - note that fields aren't available to the template file unless they are displayed in the Manage Display tab, this might be the root cause of your problem.

  • Definitely use in conjunction with montaigne's answer. – Darvanen Mar 12 '15 at 5:16
  • Thank you both, that's a good start. I did everything you suggested and now I have dpm($variables) returning the node object to my search tpl. The node object has my field 'field_type' listed as such (using var_dump): array 0 => 'node' => object (stdClass) public 'field_type' array. Now, I've tried everything I could think of to use it in my code (examples: $result['node']['field_type'], $result[0]['node']['field_type'], $field_type...how do I access a variable of this type now that I have it? – Sage Mar 12 '15 at 9:44
  • You'll need a print statement in your template file, something like print render($variables['field_type']) I think. – Darvanen Mar 12 '15 at 10:47
  • So that's how I would reference it from my tpl.php file you're saying. It must not be being passed correctly, still. I updated my code in my original post above for template.php if you care to take another look at what I've done so far. It seems like maybe I shouldn't have two preprocess functions, not sure, one has an 's' and the other doesn't. – Sage Mar 12 '15 at 20:15
  • nevermind, I got it working. I really appreciate your help! – Sage Mar 12 '15 at 21:37

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.