How Gatsby's Drupal source plugin handles file entity relationships for multilingual sites

By edisch, 3 August, 2021

Drupal has had core support for translation since Drupal 8. But some entity types are not translatable due to their nature. Files are a good example of this. Because files are inherently references to the filesystem they don't lend very well to the translation paradigm. However you can wrap files using Media if you want to have translatable fields related to a particular file.

Files are not translatable

So how does this work in Gatsby? Well, by default the gatsby-source-drupal plugin relates referenced content by trying to find the related entity in the current node language. This is best visualized with an example. Consider a node (let's call this "Blog 1") which references a piece of media (let's call this "Image 1"). gatsby-source-drupal will attempt to find the English version of "Image 1" to relate to the English version of "Blog 1" and respectively find the Spanish version of "Image 1" to relate to the Spanish version of "Blog 1".

Traditional relationships where translatable entities are referenced

How does this work for files, where there's only ever one version or "translation" sourced? Following this logic Gatsby would look for an English and Spanish version of the file.

Traditional relationships where files are referenced using current source plugin

But this means that files for Spanish content won't show up! How do we fix this? Well we can tell Gatsby to always use the default language for "non-translatable entities".

How untranslatable entities could work after adding a "nonTranslatableEntities" option

This is how the PR from @apmsooner tackles this issue. By adding a configuration option languageConfig.nonTranslatableEntities you can tell Gatsby to always reference the languageConfig.defaultLanguage version of a particular entity.


// gatsby-config.js

module.exports = {
  // ...
  plugins: [
    {
      resolve: `gatsby-source-drupal`,
      options: {
        baseUrl: 'https://example.com',
        languageConfig: {
          defaultLanguage: 'en',
          enabledLanguages: ['en', 'es'],
          translatableEntities: ['node--article'],
          nonTranslatableEntities: ['file--file'],
        },
      },
    },
  ],
  // ...
}

This PR hasn't been merged yet but it has only been open for a few days. I believe within the next few weeks we should see this improvement come into the core gatsby-source-drupal.

Update (25-08-2021): the PR has been merged and is released under the 4.13.0-next.1 version.

Author