Posted by Kameleonten on December 2, 2009 at 10:15pm
I searched everywhere to find out on how to group drupal search results by content type and repetitively just ended up with answers which involving views like if views was the solution to everything. However, Eric at thedrupalblog.com has a much more clever solution for my problem that I would like to share:
For D5
<?php
function MYTHEME_search_page($results, $type) {
$html = '<dl class="search-results">';
// get a list of node types
$nodeTypes = node_get_types();
// loop through results, group by type
$resultTypes = array();
foreach ($results as $result) {
$resultTypes[$result['node']->type][] = $result;
}
// create fieldsets for each type
foreach ($resultTypes as $resultType => $resultTypeResults) {
$value = "";
// loop through entries
foreach ($resultTypeResults as $entry) {
$value .= theme('search_item', $entry, $type);
}
// add fieldset
$html .= theme('fieldset',
array(
'#title' => $nodeTypes[$resultType]->name,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#value' => $value,
)
);
}
$html .= '</dl>';
$html .= theme('pager', NULL, 10, 0);
return $html;
}
?>For D6
<?php
function TEMPLATENAME_preprocess_search_results(&$variables) {
$variables['search_results'] = '';
// get a list of node types
$nodeTypes = node_get_types();
// loop through results, group by type
$resultTypes = array();
foreach ($variables['results'] as $result)
{
$resultTypes[$result['node']->type][] = $result;
}
// create fieldsets for each type
foreach ($resultTypes as $resultType => $resultTypeResults)
{
$value = "";
// loop through entries
foreach ($resultTypeResults as $result)
{
$value .= theme('search_result', $result, $variables['type']);
}
// add fieldset
$variables['search_results'] .= theme('fieldset',
array(
'#title' => $nodeTypes[$resultType]->name,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#value' => $value,
)
);
}
$variables['pager'] = theme('pager', NULL, 10, 0);
// Provide alternate search results template.
$variables['template_files'][] = 'search-results-'. $variables['type'];
}
?>
Comments
TEMPLATENAME
In Drupal 6, I had to use the theme name instead of TEMPLATENAME:
function TEMPLATENAME_preprocess_search_results(&$variables) {I found using the D6 solution
I found using the D6 solution actually removes $variables being set within the hook_preprocess_seach_result(&$variables);
The reason for the loss of data is due to the theme layer being called twice, while the preprocess_search_result is called the data becomes lost as it's not being handled via apachesolr.