2

I'm using Drupal 7, the basic Drupal search tool. I like what it returns as far as the actual node pages. However, I want to customize the main search results page that shows a list of results after a user enters a term to search by.

Right now it shows a list with a linked title to the node (fine), and then a 'summary' (if you can call it that) that's sort of a jumbled mess of the URL, part of the description field, and some other fields. I just want the description to show up there. I'd also like to add the date field and the associated image for the node in the search results. So I've discovered that I need to customize template.php mytheme_preprocess_search_results function and also search-results.tpl.php (results NOT result).

But I can't figure out how to parse out individual items from what the database is returning so I can make sure to only print those out in the tpl file. I did a var_dump on $search_results and see it returns a big 'ol chunk of HTML and database data...but how do I return just individual fields from the database and print those out?

Right now it just does a print $search_results.

I tried print $title and print $url but drupal complains it doesn't recognize those vars. So what are the vars and how can I use them to display what I want?

I'm also noticing it's showing the author and date for each node in the search results (although I hid this information in the content type)...so I'd also like to hide that information in the search results.

I've found a bunch of stuff online that led me to those two files I referenced, for changing the search results...but nothing that specifically addressed how to grab and use individual fields from the database. I also don't want to use a module, I want to do this with template.php and the tpl.php files if possible.

Thanks for your help in advance!

3

To change the snippet (that jumbled mess you mention), use theme_preprocess_search_result() (you want to override each search result, not the page of search results). Explicitly set $variables['snippet'] to the value you want to display.

A common tactic is to use the node's teaser view.

function mytheme_preprocess_search_result($variables) {
  $teaser = node_view($variables['result']['node'],'teaser');
  $variables['snippet'] = drupal_render($teaser);
}

You can manage the node's teaser in Admin -> Structure -> Content Types -> Your content type -> Manage Display -> Teaser as always.

There's a no-code approach also. While you're managing displays, check out the expandable fieldset for 'Custom display settings' and check the one for 'Search results highlighting input'. Here you can control the fields you want to display, but Drupal will still show only a snippet, and it will highlight where in the node your search terms show up. Ditch the preprocess function in this case.

Here is one more example with the theme_preprocess_search_result() function that shows the first 160 characters of the body field: https://alastaira.wordpress.com/2011/01/07/customising-the-search-results-snippet-in-drupal-7/ (note that this one does not check for language).

  • FYI: I was able to add $variables['info'] = ''; to remove the author and date information too; sweet. – Sage Feb 5 '15 at 9:22
  • excellent; thanks for approving the answer even though I left something out – calebtr Feb 5 '15 at 9:25

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.