The JSON:API Menu Items and JSON:API Search API modules have different route names than json:api, so the hook does not fire when requesting it.

Comments

Abdalhamid created an issue. See original summary.

Abdalhamid’s picture

Abdalhamid’s picture

fixing patch name

Abdalhamid’s picture

ruuds’s picture

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.

jrb’s picture

Title: Add support to JSON:API Menu Items module » Add support for JSON:API Menu Items, JSON:API Search API, other modules
Issue summary: View changes
StatusFileSize
new559 bytes

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 in ResponseSubscriber::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).

  • Ruuds committed 8709a8e2 on 1.0.x
    Issue #3341933 by Abdalhamid, jrb, Ruuds: Add support for JSON:API Menu...
ruuds’s picture

Status: Needs review » Fixed

Thanks, that would do the trick indeed! Creating a new release shortly.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.