Skip to content

Conversation

@Auspicus
Copy link
Contributor

@Auspicus Auspicus commented Aug 26, 2020

Description

We're currently using gatsby-source-drupal to pull content from our D8 site and we got stuck trying to pull content from another language because it gets overwritten by the last source plugin in the list. eg. gatsby-config.js

...
{
  resolve: `gatsby-source-drupal`,
  options: {
    baseUrl: `http://example.com`,
  },
},
{
  resolve: `gatsby-source-drupal`,
  options: {
    baseUrl: `http://example.com/es`,
  },
},
...

Documentation

  • Add information about the new parameters language and defaultLanguage to the README.md

Related Issues

Addresses #10020

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Aug 26, 2020
@Auspicus Auspicus changed the title feat(gatsby-source-drupal): Add multilingual support by prefixing nodes not in the default language with their langcode WIP: feat(gatsby-source-drupal): Add multilingual support by prefixing nodes not in the default language with their langcode Aug 26, 2020
@Auspicus
Copy link
Contributor Author

Auspicus commented Aug 26, 2020

Things left to do:

  • see how this plays with fast builds
  • confirm backwards compat
  • add documentation to README.md

// Default defaultLanguage to `en`
defaultLanguage = defaultLanguage || `en`

const languagePrefix = defaultLanguage !== language ? language : ``
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to just pass in languagePrefix. I think that makes the most sense from a DevX perspective. "I am trying to use an endpoint with a /es/ prefix, I put languagePrefix: es into the gatsby-config"

Copy link
Contributor

@smthomas smthomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting pretty close, but there are a few things we need to consider before this can be merged:

  1. Right now this expects a JSON:API endpoint to only return entities from one language. I have plans to be able to bundle multilingual endpoints into a single endpoints (for efficiency reasons and build time performance). Ideally, we could create a way to use the langcode returned from the entity instead of just using a constant language prefix from gatsby-config.

  2. This isn't going to work with incremental builds, fastbuilds, or instant preview yet. In utils.js there is a handleWebhookUpdate function that needs some updating. I think this is the start of what needs updating. Right now it just tries to use the UUID to find the node, we need to make sure it appends the same language prefix here so it finds the correct node.

let allData
try {
const data = await axios.get(`${baseUrl}/${apiBase}`, {
const data = await axios.get(`${baseUrl}/${languagePrefix}/${apiBase}`, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this always work? It seems that this will default to "/en/jsonapi" which doesn't seem to work out of the box with JSON:API.

I think the better approach might be to default to the current URL (baseURL/apiBase) unless a language prefix is added to gatsby-config.js. If it's added then it can be appended to the URL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw the same thing, fortunately Drupal does redirect /// to / so in the case a languagePrefix is not specified it would change to baseUrl//jsonapi rather than baseUrl/es/jsonapi which both work. I don't like forcing a redirect in default case though so I will definitely refactor this so that it's not required when langaugePrefix is not specified.

@Auspicus
Copy link
Contributor Author

Got fast builds working with the translated content there's just some funkiness going on with the relationships in the translated content that needs to be resolved. Most likely the references are just missing their prefixes in some cases.

@vladar vladar added status: needs core review Currently awaiting review from Core team member topic: source-drupal Related to Gatsby's integration with Drupal and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Aug 28, 2020
@Auspicus Auspicus marked this pull request as draft August 28, 2020 21:21
@Auspicus
Copy link
Contributor Author

Auspicus commented Aug 30, 2020

@smthomas With the administer languages permission it seems like we could use /jsonapi/configurable_language/configurable_language to fetch the languages available for the site and then the /jsonapi/language_content_settings/language_content_settings to figure out which entities can be translated. There's an attribute on language_content_settings called language_alterable which tells us that the entity might have content in another language.

I might rework this PR to do that but for the mean time we are using a forked version that just specifies the languagePrefix directly in the gatsby-config.js. I'll push the final changes for this PR tomorrow which should make fast builds work with it and then start working on getting it progamatically figuring out which languages are available with what I described above.

If we only have to pull translations for content that has translations available it should make the process a lot faster rather than including 2 different gatsby-source-drupal plugins and having it pull the entire site in both languages.

This request will get all translatable entities:

/jsonapi/language_content_settings/language_content_settings?filter[language_alterable]=true

@Auspicus
Copy link
Contributor Author

Closed in favor of #26720

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: needs core review Currently awaiting review from Core team member topic: source-drupal Related to Gatsby's integration with Drupal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants