I'm using the jsonapi.entity.to_jsonapi servcie (EntityToJsonApi) to provide entities in json format to the theme.

I wonder is it possible to also do includes like in the request string when using the service (so I don't have to manually look up entity references etc)?

Thanks in advance

Comments

miiimooo created an issue. See original summary.

Wim Leers’s picture

I'd recommend to not use this. There's a reason that we moved it out of jsonapi and into jsonapi_extras in #2982455: Add `jsonapi.entity.to_jsonapi` service.

If you really want to use this, update to the latest JSON API Extras, the jsonapi_extras.entity.to_jsonapi service there has support for includes since #2995142: Support includes the EntityToJsonapi service.

Wim Leers’s picture

Sorry, my answer was not complete enough. The answer to "Why?" can be found in #2982210: Move EntityToJsonApi service to JSON API Extras.

miiimooo’s picture

Thanks for the quick answer. Maybe I'm reading too much into your comments, but did you mean you wouldn't recommend to use jsonapi/extras to attach json in a preprocess/controller so it can be used in the theme or did you mean it's a bad idea at all? We're discussing this so would be great to get your input. Attaching drupalSettings seemed to like it could be fast and lightweight to provide initial data in a partially decoupled site. Maybe needs it's own issue. I'll try now with the JSON API extras.

Wim Leers’s picture

You're not reading too much into it — I personally believe that for consistency and maintainability reasons, the "entity to JSON API" service should ideally not exist at all. But you cite one of the few cases where it could be justified. In that case, it should IMHO be implemented as a subrequest, so that in the end, all the JSON API module does is provide responses to certain requests. I explained these concerns in #2874509: Provide service to simplify generating a JSON API representation of an entity in PHP code a long time ago.

Attaching drupalSettings seemed to like it could be fast and lightweight to provide initial data in a partially decoupled site

This could indeed be a reasonable approach. For now, just use the jsonapi_extras.entity.to_jsonapi service. It's up to that service to be improved, you shouldn't have to worry about it :) Sorry for the distraction!

Status: Fixed » Closed (fixed)

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