Customising the Search Results Snippet in Drupal 7

I use the synonyms module in several Drupal websites. This excellent, simple module allows you to define multiple synonyms for each taxonomy term. There are various reasons why you may want to do this, but the main reason for me is to allow searches for synonymous taxonomy terms to all return the same set of matching node results.

For example, on the website, young people can search for health information on a number of topics. Considering the nature of medical taxonomy, and the target audience, we wanted to include a wide variety of synonym, slang, and misspelled versions of each taxonomy term. That way, searches for “acne”, “blackheads”, “zits”, etc. all return the page about spots (which is tagged with the primary term “spots”, to which all those synonyms are attached), even if none of those alternative words are actually used on the page itself.

One problem I came across when using the synonyms module is the snippet of text displayed in the search results listing, which is created by the core search module’s search_excerpt() function. By default, search_excerpt()  scans the text of the node and creates a snippet highlighting the matching text found from the search input. However, when using the synonyms module, the supplied search input might not actually appear anywhere in the text of a node at all (since it may just be a synonym of a term that is attached to the page). In these cases, the search result listing was just displaying ‘…’

There is no way of overriding search_excerpt(), and there is no search_excerpt_alter() function that I could find, so instead I used template_preprocess_search_result() to modify search results that had ‘…’ snippets to instead include the first 160 characters of the body text of the relevant node, which is available in the $variables[‘result’][‘node’] object, as follows:

function thf_preprocess_search_result(&$variables) {
$result = $variables[‘result’];
if(strlen($result[‘snippet’]) < 10) {
    $variables[‘snippet’] = drupal_substr($result[‘node’]->body[‘und’][0][‘value’], 160);
This entry was posted in Drupal and tagged . Bookmark the permalink.

2 Responses to Customising the Search Results Snippet in Drupal 7

  1. Hi , thanks for the snippet.
    Altho , I’m wondering why you don’t include the starting position for your drupal_substr function.
    Right syntax is > drupal_substr($text, $start, $length = NULL)

    So in your example the line would be:
    $variables[‘snippet’] = drupal_substr($result[‘node’]->body[‘und’][0][‘value’], 0, 160); to only include the first 160 chars of body as a search snippet.

    Right ?

  2. Pingback: A Drupal 7 keresési eredmények oldalának formázása – Hajas Tamás

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s