Support JSON:API filters

Created on 20 February 2020, over 4 years ago
Updated 18 July 2024, about 2 months ago

Hi ,

Thanks for the awesome module. With the help of this module I have created a custom route to display multiple content types. And I also checked the include,sort and pagination. Everything is working as expected. But Am trying to add a filter condition in the API URL but it doesn't seems to be working.

Can anyone help me out how can we add the filter parameter in the API call ?

Thanks in Advance.

โœจ Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia suresh7787

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฏ๐Ÿ‡ต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 about 2 months ago
  • ๐Ÿ‡ฏ๐Ÿ‡ต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).

  • Merge request !17Draft: Filter support โ†’ (Open) created by ptmkenny
  • Pipeline finished with Success
    about 2 months ago
    Total: 186s
    #223075
  • Pipeline finished with Success
    about 2 months ago
    Total: 195s
    #223113
  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny

    An important note for filtering: the order of _jsonapi_resource_types in mymodule.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.

  • Pipeline finished with Success
    about 1 month ago
    Total: 157s
    #235374
Production build 0.71.5 2024