Dispatch events when processing resource request that allows filters and sorting to be altered

Created on 19 September 2024, 9 days ago
Updated 20 September 2024, 8 days ago

Problem/Motivation

Currently there is no way to customize the filters or sorting that is applied to the query when processing the resource request. So adding integrations with other functionality or modules such as search_api_location as proposed in Provide a mechanism for supporting search_api_location Needs work can only be done by adding specific code to IndexResource::applyFiltersToQuery(). In addition, other modules can not act on filter query params, because they are already consumed by this method, any any unknown filter properties cause an error. The same applies to sorting in IndexResource::applySortingToQuery().

One way to allow the filters and sorting to be altered would be to dispatch events that have access to the filter/sort properties and queries, and subscribers would be able to alter those as needed.

Steps to reproduce

Proposed resolution

  • Create event constants APPLY_FILTERS and APPLY_SORTING
  • Create event classes ApplyFiltersEvent and ApplySortingEvent that have access to getting/setting the filters/sorting and altering the query
  • Dispatch events in the appropriate places in applyFiltersToQuery() and applySortingToQuery()

Remaining tasks

User interface changes

API changes

Event subscribers can subscribe to Drupal\jsonapi_search_api\Event::APPLY_FILTERS and Drupal\jsonapi_search_api\Event::APPLY_SORTING.

Handlers are passed the respective Drupal\jsonapi_search_api\Event\ApplyFiltersEvent or Drupal\jsonapi_search_api\Event\ApplySortingEvent events, which have method to get and set the filters/sorting array, as well as get the search api query.

Data model changes

Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024