- Issue created by @Abdalhamid
- π³π±Netherlands Ruuds
Looking at this, i was thinking... Wouldn't it be enough to just check if \Drupal\jsonapi\Routing::isJsonApiRequest(...) is TRUE?
Jsonapi_menu_items also implements the requirements for isJsonApiRequest to return TRUE. And this would also open up support for other related JSON:API requests.
- πΊπΈUnited States jrb Raleigh-Durham Area, NC, USA
We'd like to use this module with the JSON:API Search API module, but are seeing the same problem.
Wouldn't it be enough to just check if \Drupal\jsonapi\Routing::isJsonApiRequest(...) is TRUE?
Jsonapi_menu_items also implements the requirements for isJsonApiRequest to return TRUE.
That does not look like it would work. That
Routes::isJsonApiRequest()
method basically checks this:str_starts_with($defaults[RouteObjectInterface::CONTROLLER_NAME], static::CONTROLLER_SERVICE_NAME)
So, it's looking to see if the "_controller" defaults setting starts with "jsonapi.entity_resource". That's not the case for the JSON:API Menu Items module (or the JSON:API Search API).
Here's what the the route defaults look like for core and the other two modules.
Core - Taxonomy terms for a vocabulary
Route: jsonapi.taxonomy_term--keywords.collection Array ( [_controller] => jsonapi.entity_resource:getCollection [resource_type] => taxonomy_term--keywords [_is_jsonapi] => 1 )
JSON:API Search API - jsonapi_search_api
Route: jsonapi_search_api.index_example_index Array ( [_jsonapi_resource] => Drupal\jsonapi_search_api\Resource\IndexResource [_jsonapi_resource_types] => Array ( [0] => node--page ) [index] => a20ddf02-ab33-4ab4-a0bb-67abc14be25f [resource_type] => search_api_index--search_api_index [_is_jsonapi] => 1 )
JSON:API Menu Items - jsonapi_menu_items
Route: jsonapi_menu_items.menu Array ( [_jsonapi_resource] => Drupal\jsonapi_menu_items\Resource\MenuItemsResource [_is_jsonapi] => 1 )
In each case,
_is_jsonapi
is set to TRUE, so it looks like that would be the best thing to check. I've attached a patch that adds a check for this to the existing conditional inResponseSubscriber::onResponse()
, but it may be possible to just replace the 3 checks with this single check instead:if ($this->routeMatch->getRouteObject()->getDefault('_is_jsonapi')) {
That would work for core and these 2 modules.
I've updated the title and description for this issue to include the JSON:API Search API module (and potentially other modules).
- Status changed to Fixed
about 1 year ago 11:55am 12 October 2023 - π³π±Netherlands Ruuds
Thanks, that would do the trick indeed! Creating a new release shortly.
Automatically closed - issue fixed for 2 weeks with no activity.