Javascript 如何创建动态URL';来自Drupal JSON:API响应中术语关系的Gatsby中的s?

Javascript 如何创建动态URL';来自Drupal JSON:API响应中术语关系的Gatsby中的s?,javascript,graphql,drupal-8,gatsby,Javascript,Graphql,Drupal 8,Gatsby,我使用Drupal8作为无头CMS,Gatsby生成静态页面 在Drupal中,我设置了一些节点类型(文章、图像、其他…)。所有实体都有品牌和类别术语关系 类别为单选,品牌为多选 盖茨比用我的模板在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/catego

我使用Drupal8作为无头CMS,Gatsby生成静态页面

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

类别
为单选,
品牌
为多选

盖茨比用我的模板在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/
带有所选术语的文章和图像节点将显示在相应的url上

我遇到的问题是,由于我将品牌术语改为多选,一些URL没有被创建

由于
field\u 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,
        },
      })
    })
  })
}
本质上,
taxonomyterbrand.name
的值与
node.relationships.field\u brand[0].name
在数据传递到模板时的值相同,但我不能使用
taxonomyterbrand.name
,因为
createPage
中的
路径
allNodeArticle.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,
      },
    })
  });
})

让它工作起来!谢谢使用分类术语“name”的唯一一点是它不是URL格式的,或者与小写字母不一致,所以我目前的方法不是很可靠(因为我必须为URL和传递上下文设置这些字符串的格式)。作为下一步,我必须找到一种方法将这些术语与API中它们的id联系起来,但很高兴现在它能够正常工作。您可以尝试一些类似path auto模块的方法。这将允许您在Drupal中设置自定义路径配置,然后当您将内容拉入Gatsby时,您可以使用Drupal告诉您使用的内容。这将帮助您保持一致性。