Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 291711

Views current_page incorrect when out of range

$
0
0

Problem/Motivation

The current page count in views is incorrect when requesting a page that is past the page limit based on the number of items.

For example in a view the current page is handled as follows:
?page=0: current_page == 0 rendered as 'Page 1'
?page=1: current_page == 1 rendered as 'Page 2'
?page=2: current_page == 1 rendered as 'Page 2'
?page=3: current_page == 2 rendered as 'Page 3'

The behavior I would expect is that the current page increments sequentially from 0 in code and from 1 in the rendered output.

Proposed Resolution

The current code below sets the current page to the last page of results in Drupal\views\Plugin\views\pager\SqlBase::updatePageInfo().

      // See if the requested page was within range:
      if ($this->getCurrentPage() >= $pager->getTotalPages()) {
        $this->setCurrentPage($pager->getTotalPages() - 1);
      }

This is incorrect as the view is not listing the last page of results, but an empty result set. Instead, the current page should remain out of range.

Instead the proposed behavior is:
1. For a full pager (or one where a COUNT query is performed) we know the total result count so we can set any out of range page count request to the last page + 1.
2. For a mini pager we don't know the total page count, so when it's of range the page count simply increments irrespective of the actual number of pages.

This logic is below:

      // See if the requested page was within range:
      $total_pages = $pager->getTotalPages();
      if ($this->getCurrentPage() >= $total_pages) {
        // When out of range, set the current page to the last page + 1.
        $this->setCurrentPage($total_pages);
        // Create a new pager with an extra empty page.
        $this->pagerManager->createPager($total_pages * $items_per_page + 1, $items_per_page, $this->options['id']);
      }

API changes

The following views tests have been updated for the above behavior:

  • Functional/Plugin/MiniPagerTest.php
  • Functional/Plugin/PagerTest.php
  • Kernel/Handler/AreaDisplayLinkTest.php

Viewing all articles
Browse latest Browse all 291711

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>