- ๐ฏ๐ตJapan ptmkenny
Discussion with @mglaman on Slack:
ptmkenny 8 hours ago Is it possible to use regular JSON:API filters (like /jsonapi/my-resource&filter[group-name][group][conjunction]=OR&filter[filter-name][condition][path]=n... on a JSON:API Resource created with the JSON:API Resources module? It seems they are not supported out of the box, but is it possible to get filter support working, or do I have to add my own parameters to the resource to use instead of filters? mglaman 7 hours ago I thought itโd work, as we expand filters like normal mglaman 5 hours ago I guess Iโm wrong, we donโt provide methods for this mglaman 5 hours ago it was added in jsonapi_search_api but these helper methods should be applied in jsonapi_resources https://git.drupalcode.org/project/jsonapi_search_api/-/blob/8.x-1.x/src/Resource/IndexResource.php?ref_type=heads#L159-176 mglaman 5 hours ago https://git.drupalcode.org/project/jsonapi_search_api/-/blob/8.x-1.x/src/Resource/IndexResource.php?ref_type=heads#L190-197 mglaman 5 hours ago https://git.drupalcode.org/project/jsonapi_search_api/-/blob/8.x-1.x/src/Resource/IndexResource.php?ref_type=heads#L208-212
- Status changed to Needs work
6 months ago 6:08am 12 July 2024 - ๐ฏ๐ตJapan ptmkenny
I started on an MR for this.
First, you need to add the following code to the
process()
method of your custom JSON:API resource:if ($request->query->has(Filter::KEY_NAME)) { $this->applyFiltersToQuery($request, $entity_query, $cacheability); } if ($request->query->has('sort')) { $this->applySortingToQuery($request, $entity_query, $cacheability); }
However, the filters don't currently work because I don't understand how to get the resource type (see the comment in the MR code).
- ๐ฏ๐ตJapan ptmkenny
An important note for filtering: the order of
_jsonapi_resource_types
inmymodule.routing.yml
is important._jsonapi_resource_types: ['node--base_for_entity_query', 'other_entity--bundle_type']
The first entity type listed must be the base for the entity query; if you put them in a random order, filters like
entity_reference_field.referenced_entity_field
will fail. - ๐ฏ๐ตJapan ptmkenny
@sarbazx No, it's not fixed. If it was fixed, the status would be "Fixed", not "Needs work".
I wrote the MR and it works for me. But it doesn't have any tests (which probably need to be created for it to be merged), and no one else has reported using it successfully, so I don't know how well it works for other use cases. For my own site, it's working great.