Following code returns "4.6116860184274E+18" (i.e. 2^62) sometimes for views total rows count. when there are about 500 results for the view.

use Drupal\views\Views;
$view = Views::getView('export_patient_csv');
$view->execute('patient');
$rows = $view->total_rows;
dpm($rows, 'rows');

What is the correct way to get total_rows for the view? This could be possible bug, because the same code would give back proper result otherwise.

EDIT
===
1. The display 'patient' is of type REST Export

Comments

d34dman’s picture

D34dMan created an issue. See original summary.

d34dman’s picture

Issue summary: View changes
d34dman’s picture

When inspecting views.tokens.inc it shows that the token "total-rows" is returned as count($view->result);. This makes me wonder if $view->total_rows has any significance at all.

d34dman’s picture

Issue summary: View changes
dawehner’s picture

use Drupal\views\Views;
$view = Views::getView('export_patient_csv');
$view->get_total_rows = TRUE;
$view->execute('patient');
$rows = $view->total_rows;
dpm($rows, 'rows');

This is basically what you need :)

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

d34dman’s picture

Thanks for the quick response, much appreciated.

However, i tried the suggestion and its still giving back 2^62.

Here is the code i used for testing in devel/php.

use Drupal\views\Views;

$view = Views::getView('export_patient_csv');
$view->get_total_rows = TRUE;
$view->execute('patient');
$rows = $view->total_rows;
dpm($rows, '$view->total_rows');

$view = Views::getView('export_patient_csv');
$view->setItemsPerPage(0);
$view->execute('patient');
$total_rows = count($view->result);
dpm($total_rows, 'count($view->result)');

And the output was

$view->total_rows => 4.6116860184274E+18
count($view->result) => 522
d34dman’s picture

$view->get_total_rows says in todo to move it to count query. That inspired me to try the query way of getting the count.

The following code gives back the correct count.

use Drupal\views\Views;
$view = Views::getView('export_patient_csv');
$view->build('patient');
$q = $view->query->query()->countQuery()->execute()->fetchAssoc();
$count = reset($q);
dpm($count, '$view->query->query()->countQuery()->execute()->fetchAssoc()');
vierlex’s picture

You can also use fetchField(); to save yourself the reset() function call afterwards :)

$total_rows = $view->query->query()->countQuery()->execute()->fetchField();
cilefen’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

anselmo.temple@tell.com’s picture

Hi all I, How to sort the workflow,

anselmo.temple@tell.com’s picture

Hi all, How to sort the workflow,

therobyouknow’s picture

Pleased to confirm that this solution also works in Drupal 9, 9.3.3: https://www.drupal.org/project/drupal/issues/2797565#comment-11907615

I haven't tried the others yet. But thank you to the solution provider, the question asker and everyone else who contributed.

vensires’s picture

The last responses provided are truly the correct ones. Just adding here for anyone coming from search engines that in case your View is an indexed view using search_api (ex. Solr), then the correct approach is this one:

  $view = Views::getView('the_id_of_the_view');
  $view->build('the_display_of_the_view');
  $total_rows = $view->query->query()->getResults()->getResultCount();