search results as teasers (make data not show up)
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
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
Here's a cleaner version for Drupal 5.x:
<?phpfunction 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;
}
?>
There's a bug in the above
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.
<?phpfunction 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