gpt4 book ai didi

javascript - 如何根据 Drupal JSON 中的术语关系在 Gatsby 中创建动态 URL :API response?

转载 作者:行者123 更新时间:2023-12-02 23:24:31 24 4
gpt4 key购买 nike

我使用 Drupal 8 作为 headless CMS,并使用 Gatsby 生成静态页面。

在 Drupal 中,我设置了一些节点类型(文章、图像、其他...)。所有实体都具有品牌类别术语关系。

category 是单项选择,brand 是多项选择

Gatsby 根据我的模板在 URL 上创建页面。

http://example.com/category-1/brand-1/
http://example.com/category-1/brand-2/
http://example.com/category-2/brand-1/
http://example.com/category-2/brand-2/

包含所选术语的文章和图像节点将显示在相应的网址上。

我遇到的问题是,自从我将品牌术语更改为多选后,某些网址并未创建。

因为 field_brand[0].name Gatsby 只会为文章节点上第一个选择的品牌创建 URL。

// gatsby-node.js

exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const pageTemplate = path.resolve(`src/templates/pageTemplate.js`);
return graphql(`
{
taxonomyTermBrand {
field_colours
name
}
allNodeImage {
nodes {
relationships {
field_image_drop {
uri {
url
}
}
}
}
}
allNodeArticle {
nodes {
body {
processed
}
relationships {
field_brand {
name
}
field_category {
name
}
}
}
}
}
`, { limit: 1 }).then(result => {

if (result.errors) {
throw result.errors
}

result.data.allNodeArticle.nodes.forEach(node => {
const brand_path = node.relationships.field_brand[0].name;
const category_path = node.relationships.field_category.name;

createPage({
path: `${category_path}/${brand_path}`,
component: pageTemplate,
context: {
brandName: node.relationships.field_brand[0].name,
categoryName: node.relationships.field_category.name,
},
})
})
})
}

当数据传递到模板时,taxonomyTermBrand.name 的值基本上与 node.relationships.field_brand[0].name 相同,但我可以请勿使用 taxonomyTermBrand.name,因为 createPage 中的 path 位于 allNodeArticle.forEach()

是否有更好的方法或其他方法来设置路径并在这些页面上显示标记的内容?

最佳答案

我认为您可以对每篇文章的每个 field_brand 术语执行 forEach。

类似于:

result.data.allNodeArticle.nodes.forEach(node => {
const brands = node.relationships.field_brand;
const category = node.relationships.field_category.name;

brands.forEach(brand => {
createPage({
path: `${category}/${brand.name}`,
component: pageTemplate,
context: {
brandName: brand,
categoryName: category,
},
})
});
})

关于javascript - 如何根据 Drupal JSON 中的术语关系在 Gatsby 中创建动态 URL :API response?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56798862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com