首页 > 解决方案 > 有什么方法可以在 Prismic.io 自定义类型中添加键值对对象?

问题描述

Prismic 的文档有点模糊,而且他们的社区形式并没有真正的超级响应,所以我想我也会在这里问。在我拥有的自定义类型(产品)中,我希望创建一个(变体)的组中继器,我可以使用它来指定产品的“版本”(例如,视频包将具有分辨率和编解码器作为不同的选项)。我希望在前端显示前端的“漂亮”版本,并且我希望实际后端的 url 安全的小写值。

例子:

variants: {
  'H.264': 'h264',
  'ProRes': 'prores',
  'MP4': 'mp4'
}

另一个例子:

variants: {
  '44.1kHz': '44khz',
  '48kHz': '48khz',
  '96kHz': '96khz
}

解决方案是它变得有点复杂,就像上面的例子一样,我可以使用 a.replace(/\s/g, '_').replace(/./g, '').toLowerCase()将其“破解”为后端可用的代码(它会匹配)。但是,对于分辨率,它变得有点复杂:

variants: {
  '1920 x 1080': '1920x1080',
  '1080 x 1920': '1080x1920',
  '1080 (Square)': '1080x1080',
  '4K': '4k'
}

我想如果我能找到一种方法在 Prismic 中制作可重复的键/值映射,我可以创建我需要的任何变体,并让它足够抽象,以适应任何未来的变体。

这是我自定义类型的 json:

{
  "Main" : {
    "title" : {
      "type" : "StructuredText",
      "config" : {
        "single" : "heading1",
        "label" : "Title",
        "placeholder" : "Product Title"
      }
    },
    "uid" : {
      "type" : "UID",
      "config" : {
        "label" : "UID",
        "placeholder" : "The unique identifier"
      }
    },
    "category" : {
      "type" : "Link",
      "config" : {
        "select" : "document",
        "customtypes" : [ "category" ],
        "label" : "Category",
        "placeholder" : "Product Category"
      }
    },
    "featured_image" : {
      "type" : "Image",
      "config" : {
        "constraint" : { },
        "thumbnails" : [ {
          "name" : "thumb",
          "width" : 150,
          "height" : null
        }, {
          "name" : "small",
          "width" : 300,
          "height" : null
        }, {
          "name" : "medium",
          "width" : 600,
          "height" : null
        }, {
          "name" : "large",
          "width" : 900,
          "height" : null
        }, {
          "name" : "xl",
          "width" : 1200,
          "height" : null
        } ],
        "label" : "featured image"
      }
    },
    "product_primary_color" : {
      "type" : "Color",
      "config" : {
        "label" : "Product Primary Color"
      }
    },
    "short_description" : {
      "type" : "StructuredText",
      "config" : {
        "single" : "paragraph",
        "label" : "Short Description",
        "placeholder" : "The one-liner description"
      }
    },
    "price" : {
      "type" : "Number",
      "config" : {
        "label" : "Price",
        "placeholder" : "The regular price"
      }
    },
    "sale_price" : {
      "type" : "Number",
      "config" : {
        "label" : "Sale Price",
        "placeholder" : "Sale price (if any)"
      }
    },
    "on_sale" : {
      "type" : "Boolean",
      "config" : {
        "default_value" : false,
        "label" : "On sale?"
      }
    },
    "base_download_link" : {
      "type" : "Link",
      "config" : {
        "label" : "Base Download Link",
        "placeholder" : "This is the bucket-level link for product",
        "select" : null
      }
    },
    "upsell_items" : {
      "type" : "Group",
      "config" : {
        "fields" : {
          "upsell_item" : {
            "type" : "Link",
            "config" : {
              "select" : "document",
              "customtypes" : [ "product" ],
              "label" : "Upsell Item",
              "placeholder" : "This are the items that can be upsold/cross-sold"
            }
          }
        },
        "label" : "upsell_items"
      }
    },
// Variants are here ============================================
    "variants" : {
      "type" : "Group",
      "config" : {
        "fields" : {
          "variant" : {
            "type" : "Text",
            "config" : {
              "label" : "variant",
              "placeholder" : "A variant (resolution/sample rate/etc) for the download path on S3"
            }
          }
        },
        "label" : "variants"
      }
    },
    "body" : {
      "type" : "Slices",
      "fieldset" : "Slice zone",
      "config" : {
        "labels" : { },
        "choices" : {
          "embed_slice" : {
            "type" : "SharedSlice"
          },
          "image_text_slice" : {
            "type" : "SharedSlice"
          },
          "wide_text_slice" : {
            "type" : "SharedSlice"
          }
        }
      }
    }
  }
}

标签: jsonprismic.io

解决方案


目前这是不可能的,但我们将其作为开放功能请求进行跟踪,并在此处提供解决方法: https ://community.prismic.io/t/iterating-select-values-in-template-from-其他文件/818/2

谢谢。


推荐阅读