Add support for JSON:API Menu Items, JSON:API Search API, other modules

Created on 15 February 2023, over 1 year ago
Updated 12 October 2023, 12 months ago

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.

πŸ“Œ Task
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡―πŸ‡΄Jordan Abdalhamid

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

Comments & Activities

  • Issue created by @Abdalhamid
  • πŸ‡―πŸ‡΄Jordan Abdalhamid

    fixing patch name

  • πŸ‡³πŸ‡±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 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...
  • Status changed to Fixed 12 months ago
  • πŸ‡³πŸ‡±Netherlands Ruuds

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

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

Production build 0.71.5 2024