Problem/Motivation

If you use the [view:page-count] in a view that have no content, it returns 0 even if the [view:current-page] token returns 1.
So if you want to show something like "page [view:current-page] on [view:page-count]" you'll get "page 1 on 0" what is confusing.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because it is not the expected behaviour
Issue priority Minor
Unfrozen changes Unfrozen because it only changes strings.
Prioritized changes The main goal of this issue is usability.
Disruption Not disruptive as if people wanted to deal with it they'd have altered the token replacement in a way that won't break with this change.

Proposed resolution

Return 1 instead of 0 if there is no content to show in the View.

Remaining tasks

Patch, Review, Commit

User interface changes

The views that use the [view:page-count] token will show 1 instead of 0.

API changes

None.

Data model changes

None.

Comments

duaelfr’s picture

Assigned: duaelfr » Unassigned
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.9 KB
new2.55 KB

Improved tests to show the issue.

The last submitted patch, 1: view-page-count-2510076-1-tests-only.patch, failed testing.

lnadella’s picture

Status: Needs review » Reviewed & tested by the community

This patch is applied, reviewed and working as expected. After applying the patch, tokens given in Header of the view "page [view:current-page] on [view:page-count]" gives "page 1 on 1".

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.0.x and cherry-picked to 8.0.x. Thanks!

  • catch committed e3ad6a4 on 8.1.x
    Issue #2510076 by DuaelFr: The [view:page-count] token should never...

  • catch committed bd5c5bc on
    Issue #2510076 by DuaelFr: The [view:page-count] token should never...
mikeker’s picture

Status: Fixed » Needs work
+++ b/core/modules/views/views.tokens.inc
@@ -125,7 +125,7 @@ function views_tokens($type, $tokens, array $data = array(), array $options = ar
-          $replacements[$original] = (int) ceil(count($view->result) / $per_page);
+          $replacements[$original] = max(1, (int) ceil(count($view->result) / $per_page));

$view->result contains the rows being displayed on the current page. (In D7, I believe, it held the entire result set). So page-count will always be one.

See #2572355: Some view tokens ([view:page-count] and [view:total-rows]) are incorrect, which was just rerolled to apply cleanly with the fix in this issue. So, if this issue is reverted, then 2572355 will need another reroll, or 2572355-31 can be applied on top of this fix. Not sure which is cleaner...

duaelfr’s picture

I don't think we should revert this commit as it only introduces a max() call that does not change the original behavior.
If there is a problem with the token itself, it should be fixed in the other issue.
Let's keep this issue closed and move to the other one that already have a patch.

Status: Fixed » Closed (fixed)

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