From 912a9ec478e1a9fbbc5d1a77ef9f9cfdedadd1b0 Mon Sep 17 00:00:00 2001 From: kathmbeck Date: Tue, 14 Feb 2023 20:54:43 -0800 Subject: [PATCH 1/4] filter by languages --- .../gatsby-source-drupal/src/gatsby-node.ts | 51 +++++++++++++++++-- .../src/plugin-options.ts | 1 + 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/gatsby-source-drupal/src/gatsby-node.ts b/packages/gatsby-source-drupal/src/gatsby-node.ts index b4a618a189679..0e2119a2ff1aa 100644 --- a/packages/gatsby-source-drupal/src/gatsby-node.ts +++ b/packages/gatsby-source-drupal/src/gatsby-node.ts @@ -22,6 +22,7 @@ import { isFileNode, imageCDNState, } from "./normalize" +import { filter } from "lodash" const { handleReferences, @@ -512,7 +513,12 @@ ${JSON.stringify(webhookBody, null, 4)}` entityType => entityType === type ) - const getNext = async (url, currentLanguage) => { + const getNext = async ( + url, + currentLanguage, + filterByLanguages, + renamedEnabledLanguages + ) => { if (typeof url === `object`) { // url can be string or object containing href field url = url.href @@ -570,6 +576,22 @@ ${JSON.stringify(webhookBody, null, 4)}` } } + if (d.body.data && currentLanguage && filterByLanguages) { + const languageCodeForFilter = + renamedEnabledLanguages && + renamedEnabledLanguages.find( + language => language.as === currentLanguage + ) + ? renamedEnabledLanguages.find( + language => language.as === currentLanguage + ).langCode + : currentLanguage + + d.body.data = d.body.data.filter( + n => n.attributes.langcode === languageCodeForFilter + ) + } + if (d.body.data) { // @ts-ignore dataArray.push(...(d.body.data || [])) @@ -618,17 +640,27 @@ ${JSON.stringify(webhookBody, null, 4)}` `page[offset]`, String(pageOffset * pageSize) ) - return getNext(newUrl.toString(), currentLanguage) + return getNext( + newUrl.toString(), + currentLanguage, + filterByLanguages, + renamedEnabledLanguages + ) }) ) } } else if (d.body.links?.next) { - await getNext(d.body.links.next, currentLanguage) + await getNext( + d.body.links.next, + currentLanguage, + filterByLanguages, + renamedEnabledLanguages + ) } } if (isTranslatable === false) { - await getNext(url, ``) + await getNext(url, ``, false, ``) } else { for (let i = 0; i < languageConfig.enabledLanguages.length; i++) { let currentLanguage = languageConfig.enabledLanguages[i] @@ -651,7 +683,16 @@ ${JSON.stringify(webhookBody, null, 4)}` urlPath ) - await getNext(joinedUrl, currentLanguage) + const renamedEnabledLanguages = + getOptions().languageConfig.renamedEnabledLanguages + const filterByLanguages = + getOptions().languageConfig.filterByLanguages + await getNext( + joinedUrl, + currentLanguage, + filterByLanguages, + renamedEnabledLanguages + ) } } diff --git a/packages/gatsby-source-drupal/src/plugin-options.ts b/packages/gatsby-source-drupal/src/plugin-options.ts index e03aa1d1c7b4e..3359f576d3e5e 100644 --- a/packages/gatsby-source-drupal/src/plugin-options.ts +++ b/packages/gatsby-source-drupal/src/plugin-options.ts @@ -12,6 +12,7 @@ type Options = { languageConfig?: { enabledLanguages?: Array renamedEnabledLanguages?: Array + filterByLanguages?: boolean defaultLanguage?: string translatableEntities?: Array nonTranslatableEntities?: Array From d102aaf3ecf325ecf3393a9bb247a0068f91acc5 Mon Sep 17 00:00:00 2001 From: kathmbeck Date: Tue, 14 Feb 2023 21:42:26 -0800 Subject: [PATCH 2/4] add option to schema --- packages/gatsby-source-drupal/src/gatsby-node.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/gatsby-source-drupal/src/gatsby-node.ts b/packages/gatsby-source-drupal/src/gatsby-node.ts index 0e2119a2ff1aa..e2560c84a81d9 100644 --- a/packages/gatsby-source-drupal/src/gatsby-node.ts +++ b/packages/gatsby-source-drupal/src/gatsby-node.ts @@ -177,6 +177,7 @@ exports.sourceNodes = async ( fastBuilds = false, entityReferenceRevisions = [], languageConfig = { + filterByLanguages: false, defaultLanguage: `und`, enabledLanguages: [`und`], translatableEntities: [], @@ -929,6 +930,7 @@ exports.pluginOptionsSchema = ({ Joi }) => }) ) .required(), + filterByLanguages: Joi.boolean().default(false), translatableEntities: Joi.array().items(Joi.string()).required(), nonTranslatableEntities: Joi.array().items(Joi.string()).required(), }), From b6d484b48924ef0401e50959013041ad53632640 Mon Sep 17 00:00:00 2001 From: kathmbeck Date: Tue, 21 Feb 2023 15:31:23 -0800 Subject: [PATCH 3/4] readme/remove import --- packages/gatsby-source-drupal/README.md | 2 ++ packages/gatsby-source-drupal/src/gatsby-node.ts | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-source-drupal/README.md b/packages/gatsby-source-drupal/README.md index 422f30df06b93..f4b852d8c9e87 100644 --- a/packages/gatsby-source-drupal/README.md +++ b/packages/gatsby-source-drupal/README.md @@ -474,6 +474,8 @@ module.exports = { as: `uk`, }, ], + filterByLanguages: false + // add `true` here if you'd like to filter the Drupal API response by the current language translatableEntities: [`node--article`], nonTranslatableEntities: [`file--file`], }, diff --git a/packages/gatsby-source-drupal/src/gatsby-node.ts b/packages/gatsby-source-drupal/src/gatsby-node.ts index e2560c84a81d9..b5a51b50a0156 100644 --- a/packages/gatsby-source-drupal/src/gatsby-node.ts +++ b/packages/gatsby-source-drupal/src/gatsby-node.ts @@ -22,7 +22,6 @@ import { isFileNode, imageCDNState, } from "./normalize" -import { filter } from "lodash" const { handleReferences, From fbcd8b84f9a4ce97273244b7d6f71af5852e107b Mon Sep 17 00:00:00 2001 From: kathmbeck Date: Tue, 21 Feb 2023 15:32:34 -0800 Subject: [PATCH 4/4] specify boolean --- packages/gatsby-source-drupal/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-source-drupal/README.md b/packages/gatsby-source-drupal/README.md index f4b852d8c9e87..1925244b5fd80 100644 --- a/packages/gatsby-source-drupal/README.md +++ b/packages/gatsby-source-drupal/README.md @@ -475,7 +475,7 @@ module.exports = { }, ], filterByLanguages: false - // add `true` here if you'd like to filter the Drupal API response by the current language + // add a boolean `true` here if you'd like to filter the Drupal API response by the current language translatableEntities: [`node--article`], nonTranslatableEntities: [`file--file`], },