search results as teasers (make data not show up)

Last modified: August 18, 2009 - 19:01

This snippet returns search results as normal teasers, if they are nodes, off course.
A good result of this too, is that the information of the poster, posting date and other information, follows the settings as set in the theme.

But other nice features are that for example image thumbnails, or user avatars, show up too!

<?php
function mytheme_search_item($item, $type) {
  if (
module_hook($type, 'search_item')) {
   
$output = module_invoke($type, 'search_item', $item);
  }
  else {
    if (
in_array($item['type'], node_get_types())) {
     
$node->nid = str_replace("node/","",drupal_get_normal_path(str_replace("/", "", $item['link'])));
     
$node = node_load($node->nid);
     
$node->teaser = $item['snippet'];
     
$output = node_view($node, TRUE, FALSE, FALSE);
    }
    else {
     
$output = ' <dt class="title"><a href="/'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
     
$info = array();
      if (
$item['type']) {
       
$info[] = $item['type'];
      }
      if (
$item['user']) {
       
$info[] = $item['user'];
      }
      if (
$item['date']) {
       
$info[] = format_date($item['date'], 'small');
      }
      if (
is_array($item['extra'])) {
       
$info = array_merge($info, $item['extra']);
      }
     
$output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] . '</p>' : '') . '<p class="search-info">' . implode(' - ', $info) .'</p></dd>';
    }
  }

  return
$output;
}
?>

node_load not needed

kingandy - October 8, 2007 - 09:15

Looking further into the 4.7 search system it seems that the node object is submitted to the theme_search_item function as part of the $item[] array. So this part of the above code:

<?php
      $node
->nid = str_replace("node/","",drupal_get_normal_path(str_replace("/", "", $item['link'])));
     
$node = node_load($node->nid);
?>

Can be replaced with:
<?php
      $node
= $item['node'];
?>

--Andy
Developing Drupal websites for Livelink New Media

Drupal 5.x

Rob Loach - November 29, 2007 - 19:53

Here's a cleaner version for Drupal 5.x:

<?php
function mytheme_search_item($item, $type) {
  if (
in_array($item['type'], node_get_types('names'))) { // $item['type'] returns the name
   
$node = node_load($item['node']->nid);
   
$output = node_view($node, TRUE, FALSE, FALSE);
  }
  else {
   
$output = ' <dt class="title"><a href="/'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
   
$info = array();
    if (
$item['type']) {
     
$info[] = $item['type'];
    }
    if (
$item['user']) {
     
$info[] = $item['user'];
    }
    if (
$item['date']) {
     
$info[] = format_date($item['date'], 'small');
    }
    if (
is_array($item['extra'])) {
     
$info = array_merge($info, $item['extra']);
    }
   
$output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] . '</p>' : '') . '<p class="search-info">' . implode(' - ', $info) . '</p></dd>';
  }
  return
$output;
}
?>

6.x version? I tried the

j9 - April 22, 2009 - 17:52

6.x version? I tried the function w/ 6.10 but it displays the default drupal search results.

There's a bug in the above

davej - April 10, 2008 - 17:28

There's a bug in the above code which I've encountered & fixed in Drupal 5, I believe the same bug & fix applies to 4.7 too. The line:

<?php
$output
= ' <dt class="title"><a href="/'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
?>

Should be:

<?php
$output
= ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
?>

I.e. there was an erroneous slash prepended to the URL. This caused e.g. user search to return broken links. You can check theme_search_item in search.module for the correct code to go within the else block for your version of Drupal: the slash is not present there. Here's the complete 5.x version with the fix.

<?php
function mytheme_search_item($item, $type) {
  if (
in_array($item['type'], node_get_types('names'))) { // $item['type'] returns the name
   
$node = node_load($item['node']->nid);
   
$output = node_view($node, TRUE, FALSE, FALSE);
  }
  else {
   
$output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
   
$info = array();
    if (
$item['type']) {
     
$info[] = $item['type'];
    }
    if (
$item['user']) {
     
$info[] = $item['user'];
    }
    if (
$item['date']) {
     
$info[] = format_date($item['date'], 'small');
    }
    if (
is_array($item['extra'])) {
     
$info = array_merge($info, $item['extra']);
    }
   
$output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] . '</p>' : '') . '<p class="search-info">' . implode(' - ', $info) . '</p></dd>';
  }
  return
$output;
}
?>

Dave Jenkins
Circle Interactive - UK Drupal development and hosting

Where to edit

doomed - December 1, 2008 - 06:17

Just a heads up:

You can paste this code in your theme's template.php file.

If you do that, you should remove the php tags (first and last lines from the example above).
Also edit the part that says "mytheme" with the name of the theme you are using. So if your theme is called Garland2029, the first line goes like this:


function garland2029_search_item($item, $type) {

This gives me the white

bflora - June 6, 2009 - 04:56

This gives me the white screen of death when I drop it into my search.module file instead of the original function.

Still looking for a Drupal 6

doublejosh - October 4, 2009 - 09:13

Still looking for a Drupal 6 solution to this.

Drupal 6

kingandy - October 6, 2009 - 08:47

A quick look at the search_theme() hook suggests that this is now accomplished using template files - an individual search result can be themed by creating a search-result.tpl.php file in your theme folder. Beyond that, you're on your own ;)

--Andy
Developing Drupal websites for Livelink New Media

 
 

Drupal is a registered trademark of Dries Buytaert.