Support JSON:API filters

Created on 20 February 2020, almost 5 years ago
Updated 11 July 2024, 6 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

Active

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 6 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
    6 months ago
    Total: 186s
    #223075
  • Pipeline finished with Success
    6 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
    5 months ago
    Total: 157s
    #235374
  • ๐Ÿ‡น๐Ÿ‡ทTurkey sarbazx Istanbul

    Hello,

    is this fixed?

  • ๐Ÿ‡ฏ๐Ÿ‡ต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.

Production build 0.71.5 2024