Thursday, Aug 12th, 2010
By default the Drupal ApacheSolr module does not display the total number of results for a given search. This is how to add it.
For my use it makes sense to add the result total to the Search form using hook_form_[form_id]_alter().
/**
* Implementation of hook_form_[form_id]_alter().
*/
function ms_solr_site_form_search_form_alter(&$form, &$form_state) {
if ($form['module']['#value'] == 'apachesolr_search') { //Will change all searches otherwise.
if (apachesolr_has_searched() && ($response = apachesolr_static_response_cache())) {
$query = apachesolr_current_query();
$keywords = $query->get_query_basic();
$num_found = $response->response->numFound;
$form['num_found'] = array(
'#prefix' => '<div class="number-found">',
'#suffix' => '</div>',
'#value' => t('There are @number results for "@keywords"', array(
'@number' => $num_found,
'@keywords' => $keywords)
),
'#weight' => -1,
);
}
}
}
Comments
Budda replied on Permalink
Perfect! Thanks for the snippet. I chucked it in to a block instead.
joel_liquidcms replied on Permalink
Another approach might be to add the following code to search-results.tpl.php in your theme directory:
This will print "Showing 21 to 30 of 6464 results found." on your search results page.
ant replied on Permalink
Thank you for your example.
But when you have let's say 25 results, then the message says:
"Showing 20 to 30 of 25 results found."
Shouldn't it be saying
"Showing 20 to 25 of 25 results found."?
brad replied on Permalink
Yes, in that case some additional code is required to check the total count.
Sean replied on Permalink
Use this: