My site structure looks like this:
- Node content type A with entity reference field groupA pointing to a term with field groupAfield
- Node content type B with entity reference field groupB pointing to a term with field groupBfield
I want to be able to get content types A and B from a single JSON:API endpoint, and I want this endpoint to return groupB as groupA (rename groupB to groupA for nodes of content type B in the output).
This is to simplify the output for clients consuming JSON:API.
What I have tried
JSON:API usually only returns one bundle at a time. However, using the JSON:API Resources module, you can create your own custom resource that returns multiple entity types (using an entity query).
So I made a custom resource type.
I need to include the field values for both groupA and groupB, which I did like this:
This correctly gets all the data I need. So the last step is to rename
groupA in the output. This is where I'm having trouble.
Normally, I would use the JSON:API Extras module to rename
groupA for content type B. However, I cannot do this for my custom resource because then the
include statement will break (because
nodeTypeA's group does not have
nodeTypeB's group does not have
So, I need to do the renaming after JSON:API has done its processing:
$data = $this->loadResourceObjectDataFromEntityQuery($entity_query, $cacheability); $response = $this->createJsonapiResponse($data, $request, 200, );
But, this seems to be too late to change the output. Is there some way I can rename
groupA just before returning the response?