Gatsby log entity uses wrong language when creating translations through JSON:API

Created on 20 April 2021, over 3 years ago
Updated 9 May 2023, over 1 year ago

We are facing a very strange issue when creating translations through the JSON:API. We use some patches and custom code, so let me explain what's going on. We are using Gatsby fastbuilds to fetch the data from Drupal. We are using this patch as well: https://www.drupal.org/project/gatsby/issues/3171054 β†’

Problem/Motivation

Since JSON:API doesn't support creating translations, we created a workaround to get this fixed. I modified some names for clarity

class ExampleResource extends Drupal\jsonapi\Controller\EntityResource {

  public function patchIndividual(ResourceType $resource_type, EntityInterface $entity, Request $request) {
    if ($entity instanceof TranslatableInterface) {
      $acceptLanguage = $request->headers->get('accept-language');
      if (!$acceptLanguage) {
        return parent::patchIndividual($resource_type, $entity, $request);
      }
      if ($entity->hasTranslation($acceptLanguage)) {
        $entity = $entity->getTranslation($acceptLanguage);
      }
      else {
        $entity = $entity->addTranslation($acceptLanguage);
      }
    }
    return parent::patchIndividual($resource_type, $entity, $request);
  }

}

The way this works is the following; when an accept-language header is passed to the PATCH request of an entity, we either get the translation for the given language or create a new one when it doesn't exist yet. This is working fine and translations are being added as expected.

However, when we do this, the fastbuild log entities are wrongly generated. They will always use the default language instead of the translated entity.

I think this is because the PATCH request also update the default language and thus creates a log entity for that entity instead of the translated one. (hook_entity_update inside gatsby module)

Is there a way we can get this to work?

I noticed when we change the EntityToJsonApi serialize function a bit, we can get this to work, but I'm not sure if that's a valid solution. On line 114 we changed
$response = $this->httpKernel->handle($request, HttpKernelInterface::SUB_REQUEST);
to
$response = $this->httpKernel->handle($request, HttpKernelInterface::MASTER_REQUEST);

If we do that, we get the right language back, but I'm not sure why. Can someone elaborate what the MASTER vs. SUB request means?

I'm aware that this is a quite specific problem we are facing, but it seems worth the effort to investigate this, as it is not a rare use case.
I'm not sure if this is a Gatsby or jsonapi_extras module. I included the patch I made for jsonapi_extras

πŸ› Bug report
Status

Closed: outdated

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium nickmans

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024