Skip to content

Commit

Permalink
feat(gatsby-source-drupal): filter api requests by languages (#37684) (
Browse files Browse the repository at this point in the history
…#37691)

* filter by languages

* add option to schema

* readme/remove import

* specify boolean

(cherry picked from commit 310b30d)

Co-authored-by: Katherine Beck <49894658+kathmbeck@users.noreply.github.com>
  • Loading branch information
gatsbybot and kathmbeck authored Feb 23, 2023
1 parent e7499e9 commit e0c6d25
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/gatsby-source-drupal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ module.exports = {
as: `uk`,
},
],
filterByLanguages: false
// 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`],
},
Expand Down
52 changes: 47 additions & 5 deletions packages/gatsby-source-drupal/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ exports.sourceNodes = async (
fastBuilds = false,
entityReferenceRevisions = [],
languageConfig = {
filterByLanguages: false,
defaultLanguage: `und`,
enabledLanguages: [`und`],
translatableEntities: [],
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 || []))
Expand Down Expand Up @@ -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]
Expand All @@ -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
)
}
}

Expand Down Expand Up @@ -888,6 +929,7 @@ exports.pluginOptionsSchema = ({ Joi }) =>
})
)
.required(),
filterByLanguages: Joi.boolean().default(false),
translatableEntities: Joi.array().items(Joi.string()).required(),
nonTranslatableEntities: Joi.array().items(Joi.string()).required(),
}),
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-drupal/src/plugin-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Options = {
languageConfig?: {
enabledLanguages?: Array<string | RenamedLangCode>
renamedEnabledLanguages?: Array<RenamedLangCode>
filterByLanguages?: boolean
defaultLanguage?: string
translatableEntities?: Array<string>
nonTranslatableEntities?: Array<string>
Expand Down

0 comments on commit e0c6d25

Please sign in to comment.