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.
| Comment | File | Size | Author |
|---|
Issue fork jsonapi_views-3213575
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
Comment #2
tegola commentedAlong with the issue, I have created a patch. It's probably not perfect, but it gets the job done.
Comment #3
tegola commentedI'm sorry I missed a `use` in the patch. Use the updated `pagination_offset_limit_initial_support_fixed.patch`.
Comment #4
tegola commentedUsing 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.
Comment #5
bladeduBump patch #4 to work with release 1.0
Comment #6
giuseppe87 commentedComment #7
giuseppe87 commentedWith PHP 8.x I had to install the patch at https://www.drupal.org/project/drupal/issues/3143617 to avoid an error.
Comment #8
pookmish commentedThe 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.
Comment #9
noemi commentedThe 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_rowsto populate thecountproperty inmeta?Comment #10
pookmish commentedI 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.
Comment #11
noemi commented@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.
Comment #12
vhin0210 commentedI'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.
Comment #13
vhin0210 commentedHere'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.
Comment #14
vhin0210 commentedComment #15
guardiola86 commentedPatch #13 works for me
Comment #16
guardiola86 commentedWhen 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.
Comment #17
anybodyMaybe someone could prepare a MR from this? I agree this is typically very helpful!
Comment #18
anybodyComment #19
anybodyComment #20
anybodyComment #22
anybodyI turned #10 and #13 then into a MR to push things forward here. Corrected code style. Anyone willing to test and review?
Comment #23
anybody#3292906: Meta should include information about the view should then get solved as follow-up in combination.
Comment #24
anybodyWe 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:
Comment #26
vhin0210 commentedBelow is the combined branch for #23
https://git.drupalcode.org/project/jsonapi_views/-/merge_requests/21.diff
https://git.drupalcode.org/project/jsonapi_views/-/merge_requests/21