1

I am trying to create a block to put on the search results page that has a list of the content types in the search results and the number of results for that content type.

Does anyone know how this can be achieved, if it can be?

So far I've tried hooking into _preprocess_search_results but that only works with the current page of the results.

Thanks for any help with this.

3

Why aren't you using facets?

That's how blocks which list search results grouped according to a certain criteria are called and it's a built-in feature of Search API.

All you need to do is get Facet API, enable the integration module search_api_facetapi, enable the type field, set it up as a facet and enable the block.

That block then doesn't just list the content types, you can also click on it and the results are automatically limited to that type.

| improve this answer | |
  • Didn't know about it will be having a look at it now. – Nalum Dec 6 '11 at 10:50
0

EDIT: Grrr. Never mind. I mispoke. It is per page :( But it was a fun exercise at least.

search works by running each of the results through search-result.tpl.php and then aggregating them in search-results.tpl.php, which then pages them out to you. So, in theory at least, you could put some intelligence inside search-result.tpl.php that counts node types and squirrels this information away somewhere to be displayed in a block on the search results page. (Obviously, copy these tpls, don't edit the ones in modules/search :)

For example, in Drupal 6 at least, if you created this function:

function _mymodule_search_count($op='display',$type=null) {
  static $counts=array();

  if ($op=='display') {
    return $counts;
  } else {
    if ($type) {
      $counts[$type]++;
    }
  }
}

and stuffed this in the top of search-result.tpl.php:

_mymodule_search_count('count',$info_split['type']);

and then this at the top of search-results.tpl.php:

$count=_mymodule_search_count('display');
drupal_set_message('<pre>' . print_r($count,true) . '</pre>');

you would see what I mean. It's up to you to put all of this in a block, but that's pretty simple :)

| improve this answer | |
0
  1. Add a preprocess hook

  2. Copy search-results.tpl.php into your theme directory. You'll find the original in modules/search. Leave that intact and make a copy.

  3. Print the result code in the template. Edit your copy of search-results.tpl.php to print the search result count wherever you want. I put mine right below the "Search results" header tag.

<?php print $search_totals; ?>

The preporocess hook is as follows:

function YOURTHEME_preprocess_search_results(&$vars) {

// search.module shows 10 items per page (this isn't customizable)

 $itemsPerPage = 10;

  // Determine which page is being viewed

  // If $_REQUEST['page'] is not set, we are on page 1

  $currentPage = isset($_REQUEST['page']) ? $_REQUEST['page']+1 : 1;


  // Get the total number of results from the global pager

 $total = isset($GLOBALS['pager_total_items'][0]) ? $GLOBALS['pager_total_items'][0] : 0;

  // Determine which results are being shown ("Showing results x through y")

 $start = $itemsPerPage * $currentPage - ($itemsPerPage - 1);

   // If on the last page, only go up to $total, not the total that COULD be

  // shown on the page. This prevents things like "Displaying 11-20 of 17".

  //$end = (($itemsPerPage * $currentPage) >= $total) ? $total : ($itemsPerPage * $currentPage);

  $end = $itemsPerPage * $currentPage;

 if ($end > $total){
    $end = $total;
  }

  // If there is more than one page of results:

if ($total > $itemsPerPage) {

    $vars['search_totals'] = t('Displaying !start - !end of !total results', array(
      '!start' => $start,
      '!end' => $end,
      '!total' => $total,
    ));
  }

else {
// Only one page of results, so make it simpler
 $vars['search_totals'] = t('Displaying !total !results_label', array(
      '!total' => $total,
      // Be smart about labels: show "result" for one, "results" for multiple
      '!results_label' => format_plural($total, 'result', 'results'),
    ));
  }

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.