1

How can I get the total number of results on a Search API View that has a limit of 5 results to display?

Some more info: I have to make a bit more complex search where several attachments are combined on a page as a groups of results. Every group should show just the first 5 results, but also a link "see all XX results". I'm using Search API + Solr, Views with exposed fulltext search. If I use "Global: Results summary" in header or footer, it will show maximum the limit of 5 as a number.

edit 1

Search API Solr returns 'numFound' which is what I need, but how do I use it in Views?

  • I don't know if it will work on groups or totals like the Global, but you could look at Views Calc module (drupal.org/project/views_calc) – Geoff Oct 8 '14 at 3:23
  • Unfortunately, Views Calc cannot do the job. – kufeiko Oct 8 '14 at 10:43
  • Couldn't this be done with the "Pager" option? – Beebee Oct 8 '14 at 10:57
  • @Beebee Unfortunately no, as this is already an attachment and it doesn't allow its own pager. – kufeiko Oct 8 '14 at 11:23
4

So after a day, here's the result:

As J. Reynolds suggested, I finished up using $view->total_rows, but:

  • To populate $views->total_rows, you have to use a pager in your view - that was not an option with the attachments.
  • If you don't use a pager, you could force populating $view->total_rows by altering the View query before its execution like this:
    function mycustommodule_views_pre_execute(&$view) {
      $view->get_total_rows = TRUE;
    }

And after that you have that $view->total_rows!

2

Try with this type of code. You can select either attachment_before or attachment_after, and if the placement is not to your liking you may alter the views template to suit. I know this works with a normal view and I don't see why it would not work with a Search API result. And I think you can place it in a theme's template.php file if you prefer rather than a module.

function MYMODULE_views_post_execute(&$view){
    if ($view->name == "MACHINENAME_OF_VIEW") { // can slso check for display if need be
        $view->attachment_before .= <div class="result">$view->total_rows</div>
    }
}
  • Thanks for the suggestion, but $view->total_rows appears to be empty :( – kufeiko Oct 8 '14 at 13:02

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.