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.

CommentFileSizeAuthor
drupal_mini_pager_count.png7.27 KBgrimreaper

Comments

Grimreaper created an issue. See original summary.

dawehner’s picture

The 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 postExecute throw 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?

grimreaper’s picture

Thanks @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 :

  1. why the method useCountQuery() which returns FALSE is not sufficient to not execute a count query?
  2. when I comment the postExecute() method in the mini pager, the @total has the right value and in the webprofiler I see one more database request. But by extension, it should be the postExecute() method from core/modules/views/src/Plugin/views/pager/PagerPluginBase.php which should be executed, and it is empty.

Ok, I saw that in core/modules/views/src/Plugin/views/query/Sql.php line 1452:

if ($view->pager->useCountQuery() || !empty($view->get_total_rows)) {
  $view->pager->executeCountQuery($count_query);
}

and $view->get_total_rows is set by core/modules/views/src/Plugin/views/area/Result.php:

  public function query() {
    if (strpos($this->options['content'], '@total') !== FALSE) {
      $this->view->get_total_rows = TRUE;
    }
  }

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.

jamesrward’s picture

Hopefully 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.

jamesrward’s picture

jamesrward’s picture

Status: Active » Closed (duplicate)
Related issues: +#2798521: Views result summary returns float number when using the mini pager

Adding the related issue and closing.