Problem/Motivation

Cannot change pagination offset/limit using views. This is one the tentpoles of the JSON:API specitication, yet this module doesn't support it.

Steps to reproduce

Load any view with the `?page[offset]=5&page[limit]=5` querystring and notice that the pagination doesn't change.

Proposed resolution

Add support for the `page[offset]` and `page[limit]` parameters.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tegola created an issue. See original summary.

tegola’s picture

Along with the issue, I have created a patch. It's probably not perfect, but it gets the job done.

tegola’s picture

I'm sorry I missed a `use` in the patch. Use the updated `pagination_offset_limit_initial_support_fixed.patch`.

tegola’s picture

Using offset would mess up the `count` value in the meta, which is the total query count. E.g.: having 6 items in 2 pages with 5 items each would result in page 1 having 5 items and a total count of 6 (right), and page 2 having 1 item and a total count of 1 (wrong).

So I also added support for passing the page number instead of the offset. Use it with `page[number]=1&page[limit]=5`.

Again, not perfect, but fits my use case with views.

bladedu’s picture

Bump patch #4 to work with release 1.0

giuseppe87’s picture

giuseppe87’s picture

With PHP 8.x I had to install the patch at https://www.drupal.org/project/drupal/issues/3143617 to avoid an error.

pookmish’s picture

The suggested patch in #5 does not work in Drupal 10 due to symfony's ->get() method on the input bag. The input bag now requires the returned data to be a scalar (string, number, boolean, etc). But switching to use the ->all() method further breaks due to the Drupal core's view loads the pager via this line in PagerParameters. The patch mentioned in #6 and 7 does not help because it uses the same ->get() method and throws the same error, just in a different place.

noemi’s picture

The patch is working fine with 9.5.11, but the total count is not correct, since it returns the items per page + 1.

Does it make sense to use $view->total_rows to populate the count property in meta?

pookmish’s picture

I correct myself in #8 (unless it was some dependency update I needed). I've attached the patch that works for me with Drupal 10.1.4.

@noemi, I wasn't able to reproduce your incorrect count. If I set `?page[limit]=5` the returned number in the meta is 5. What could be more helpful is to have a "total" returned as well. But I wasn't able to find a quick solution for that.

noemi’s picture

@pookmish I realized the problem with the count is not directly related to this issue, but the use of an offset.
In short, the total count is equal to the count + the offset and maybe this is the expected behavior.

vhin0210’s picture

StatusFileSize
new2.62 KB

I'm getting this TypeError: explode(): Argument #2 ($string) must be of type string, array given in explode() error from PagerParameters.php.

The latest patch/diff from https://www.drupal.org/project/drupal/issues/3143617 will not accept page[number] and/or page[limit] query.

This patch added a "views_" prefix to the OffsetPage:KEY_NAME to avoid conflicts with the page key. So we can use views_page[number] and views_page[limit] instead.

vhin0210’s picture

StatusFileSize
new2.87 KB

Here's another patch, following the other query args like views-filter, changing views_page to views-page query args. So it will be views-page[number] and views-page[limit].

Also adding the new query args to the cache contexts.

vhin0210’s picture

guardiola86’s picture

Patch #13 works for me

guardiola86’s picture

When I use for example 100: &views-page[number]=0&views-page[limit]=100 I'm getting 42 results, but if I use lower numbers for limit parameter like 5, 10 or 20, number of results are more consistent. Maybe it's a timeout.

anybody’s picture

Category: Support request » Feature request

Maybe someone could prepare a MR from this? I agree this is typically very helpful!

anybody’s picture

Issue tags: +Novice
anybody’s picture

Title: Support pagination offset/limit » Support pagination offset / limit
anybody’s picture

Title: Support pagination offset / limit » Support pagination offset / limit parameters
Version: 8.x-1.0-beta2 » 8.x-1.x-dev

anybody’s picture

Status: Active » Needs review

I turned #10 and #13 then into a MR to push things forward here. Corrected code style. Anyone willing to test and review?

anybody’s picture

anybody’s picture

We should ensure unification for the parameters used!
For that we should look for global defaults (REST / JSON:API in general, in and outside of Drupal) and in core and other modules like:

https://www.drupal.org/project/jsonapi_extras:

Integrations

The JSON:API Defaults sub-module allows you to specify default input parameters (include, page[limit] and filter) for your resource collections.