Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #2
Wim LeersI'd recommend to not use this. There's a reason that we moved it out of
jsonapi
and intojsonapi_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.Comment #3
Wim LeersSorry, my answer was not complete enough. The answer to "Why?" can be found in #2982210: Move EntityToJsonApi service to JSON API Extras.
Comment #4
miiimoooThanks 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.
Comment #5
Wim LeersYou'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.
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!