Hello,
When doing tests for #2782221: Result summary Area plugin not displayed when there is no result., I found that the total count for the mini pager is not right. See attached screenshot.
I found out why the total is not good for the mini pager.
In core/modules/views/src/Plugin/views/pager/Mini.php :
public function postExecute(&$result) {
// In query() one more item might have been retrieved than necessary. If so,
// the next link needs to be displayed and the item removed.
if ($this->getItemsPerPage() > 0 && count($result) > $this->getItemsPerPage()) {
array_pop($result);
// Make sure the pager shows the next link by setting the total items to
// the biggest possible number but prevent failing calculations like
// ceil(PHP_INT_MAX) we take PHP_INT_MAX / 2.
$total = PHP_INT_MAX / 2;
}
else {
$total = $this->getCurrentPage() * $this->getItemsPerPage() + count($result);
}
$this->total_items = $total;
}
It is voluntary but I don't understand why there is this postExecute method because $this->total_items is already set to the right value.
So I think the whole method can be removed.
| Comment | File | Size | Author |
|---|---|---|---|
| drupal_mini_pager_count.png | 7.27 KB | grimreaper |
Comments
Comment #2
dawehnerThe mini pager is not working like every other regular pager. The general idea is to NOT execute a count query, but rather avoid it.
This is done using the following idea:
* Always fetch one more result, than actually needed (for example 11 instead of 10)
* In
postExecutethrow away the 11th result* As we know there has been 11th results, we can show the user the next pager link
* When there is a new page, we cannot estimate the amount of total items, so we sit it quite high
* If there is no new page, we calculate the exact amount of total items, using the current page, and the items per page.
Does this makes sense @Grimreaper?
Comment #3
grimreaperThanks @dawehner for the response.
Ok, I understand the need for the mini pager to be lighter than the full pager. And if it is expected that with a mini pager @total should not be used, ok, no problem.
But what I don't understand is :
Ok, I saw that in core/modules/views/src/Plugin/views/query/Sql.php line 1452:
and $view->get_total_rows is set by core/modules/views/src/Plugin/views/area/Result.php:
So the count query is only executed if we explicitly want to display @total in the result summary.
So why can't we remove the postExecute() method from the mini pager? So by default it is optimized if the admin doesn't want to display the @total, but if he/she wants to display the total count then there will be a count query.
Comment #4
jamesrward commentedHopefully this will be solved in #2798521: Views result summary returns float number when using the mini pager I think this can be marked as a duplicate.
Comment #5
jamesrward commentedComment #6
jamesrward commentedAdding the related issue and closing.