Hi there,

I was looking to introduce hooks/events into JSON API - my immediate use case is to add them for when entities are about to be responded with in a GET (i.e. equivalent to the Drupal entity 'view' hooks before the theming engine kicks in) but could also extend to the other verbs as well. I was wondering if the maintainers could comment on the following before I go ahead:

  • Is there any reason why adding hooks/events to JSON API would not be merged into the project? i.e. was it a deliberate decision not to have them?
  • Is there a preference for legacy style hooks vs Events? I personally find the Event Subscriber model superior and would go that way, but wouldn't do so if using legacy hooks are a deal breaker to see the patch merged.
  • I wonder if it would be better to, rather than add JSON API specific hooks/events, refactor so the aforementioned Drupal entity 'view' hooks are fired? I have no idea if that's feasible or even sensible but it could be argued rendering to JSON is not fundamentally different than HTML and perhaps JSON API should work in a way that Drupal view hooks naturally fire before responses?

Thanks.

Comments

ndavison created an issue. See original summary.

wim leers’s picture

Status: Active » Postponed (maintainer needs more info)

It's not yet clear to me what the use case is.

I have two high-level remarks:

  1. This would add significant complexity and increase the API surface. In other words: it'd make this module harder and more expensive to maintain/support.
  2. Why can't you do this with a KernelEvents::RESPONSE subscriber?
e0ipso’s picture

I would use the response event as well for that.

ndavison’s picture

So the idea would be similar to hook_ENTITY_view - acting on entities before they are sent to the "rendering" engine. In the case of JSON API, probably as late as possible before the JSON encoding happens. Currently I can't see a way to hook into when an entity is about to be rendered by JSON API (entity load is a bit too wide a net).

This may be possible using KernelEvents::RESPONSE, but I suspect getting the entities being rendered from there will lead to repeating JSON API's route -> entities logic. I'd be very happy though if this is not the case.

It's easy for me to say as a non maintainer of the module, but would adding a few events passing in entities before encoding be a significant support burden going forward? I can't imagine the API changing much (provided EntityInterface is here to stay in D8).

e0ipso’s picture

Remember that the module does not render anything. The closest to hook_ENTITY_view is to add your own entity normalizer with a higher priority and extend from the one provided in the module.

ndavison’s picture

Thanks for that suggestion I hadn't considered taking an approach like that. It will probably be very close to what I need to achieve.

wim leers’s picture

Title: Add hooks/events before responses » Add hooks/events before JSON API responses
Category: Feature request » Support request
Status: Postponed (maintainer needs more info) » Fixed
Issue tags: +API-First Initiative

Indeed.

Also don't forget about https://www.drupal.org/project/jsonapi_extras, which provides a UI for commonly desired customizations!

wim leers’s picture

Category: Support request » Feature request
Status: Fixed » Closed (works as designed)