Skip to content

Commit 84709f4

Browse files
author
Brad Carson
committed
commit first pass at module
0 parents  commit 84709f4

5 files changed

+99
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This module exists to get around the 50 page limit defined in the JSON:API module by extending the EntityResource service and overriding a couple of classes and functions. There are plenty of valid points on why you would want to do this and why you shouldn't. See this [discussion](https://www.drupal.org/project/jsonapi/issues/2793233).
2+
3+
To use: Simply change SIZE_MAX in src/Query/CustomOffsetPage.php to whatever you'd like. I've set it to 500 here since I'm using the API internally.
4+
5+
For further reference, see the [JSON API](https://www.drupal.org/docs/8/modules/json-api) documentation.
6+
7+
In the future, may want to add this as a configurable option in the UI.

jsonapi_custom_limit.info.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: "JSON:API Custom Page Limit"
2+
type: module
3+
description: "Allows you to change the imposed 50 page result limit from jsonapi module."
4+
core: 8.x
5+
package: "Web Services"
6+
version: 1.0.0
7+
8+
dependencies:
9+
- jsonapi:jsonapi (>=8.x-2.4)

jsonapi_custom_limit.services.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
jsonapi.entity_resource:
3+
class: Drupal\jsonapi_custom_limit\Controller\CustomEntityResource
4+
arguments:
5+
- "@entity_type.manager"
6+
- "@entity_field.manager"
7+
- "@jsonapi.resource_type.repository"
8+
- "@renderer"
9+
- "@entity.repository"
10+
- "@jsonapi.include_resolver"
11+
- "@jsonapi.entity_access_checker"
12+
- "@jsonapi.field_resolver"
13+
- "@jsonapi.serializer"
14+
- "@datetime.time"
15+
- "@current_user"
16+
# - { name: http_middleware, priority: 201 }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Drupal\jsonapi_custom_limit\Controller;
4+
5+
use Drupal\jsonapi\Controller\EntityResource;
6+
use Drupal\jsonapi\Query\Filter;
7+
use Drupal\jsonapi\Query\Sort;
8+
use Drupal\jsonapi\ResourceType\ResourceType;
9+
use Drupal\jsonapi_custom_limit\Query\CustomOffsetPage;
10+
use Symfony\Component\HttpFoundation\Request;
11+
12+
/**
13+
*
14+
* Overridden and extended from jsonapi module to get around the 50 results limit
15+
*
16+
*/
17+
class CustomEntityResource extends EntityResource {
18+
19+
/**
20+
* Extracts JSON:API query parameters from the request.
21+
*
22+
* @param \Symfony\Component\HttpFoundation\Request $request
23+
* The request object.
24+
* @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type
25+
* The JSON:API resource type.
26+
*
27+
* @return array
28+
* An array of JSON:API parameters like `sort` and `filter`.
29+
*/
30+
protected function getJsonApiParams(Request $request, ResourceType $resource_type) {
31+
if ($request->query->has('filter')) {
32+
$params[Filter::KEY_NAME] = Filter::createFromQueryParameter($request->query->get('filter'), $resource_type, $this->fieldResolver);
33+
}
34+
if ($request->query->has('sort')) {
35+
$params[Sort::KEY_NAME] = Sort::createFromQueryParameter($request->query->get('sort'));
36+
}
37+
if ($request->query->has('page')) {
38+
$params[CustomOffsetPage::KEY_NAME] = CustomOffsetPage::createFromQueryParameter($request->query->get('page'));
39+
} else {
40+
$params[CustomOffsetPage::KEY_NAME] = CustomOffsetPage::createFromQueryParameter(['page' => ['offset' => CustomOffsetPage::DEFAULT_OFFSET, 'limit' => CustomOffsetPage::SIZE_MAX]]);
41+
}
42+
43+
return $params;
44+
}
45+
}

src/Query/CustomOffsetPage.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Drupal\jsonapi_custom_limit\Query;
4+
5+
use Drupal\jsonapi\Query\OffsetPage;
6+
7+
/**
8+
* Value object for containing the requested offset and page parameters.
9+
*
10+
* Overrides OffsetPage from the jsonapi module to get around the 50 results limit.
11+
*
12+
*/
13+
class CustomOffsetPage extends OffsetPage {
14+
15+
/**
16+
* Max size.
17+
*
18+
* @var int
19+
*/
20+
const SIZE_MAX = 500;
21+
22+
}

0 commit comments

Comments
 (0)