【问题标题】:How can I set different label and value for dropdown fields in Contentful如何在 Contentful 中为下拉字段设置不同的标签和值
【发布时间】:2021-04-27 09:19:43
【问题描述】:

我是 Contentful 的新手,我正在尝试创建一个简单的下拉字段,在其中为每个选项定义不同的值和标签。但我似乎不知道该怎么做。

我通过创建 短文本字段、将 外观 设置为 Dropdown 并选中 Accept only specified 验证下的值

但它只允许我设置标签部分,它也会自动用作值。由于我的很多值都转换为枚举,因此对于普通用户来说,在下拉菜单中使用 TYPE_CAMPAIGN 而不是“Campaign”之类的内容很奇怪。

作为我正在寻找的示例,在 Advanced Custom Fields (WordPress) 中,我可以通过键入 TYPE_CAMPAIGN : Campaign 来完成,这将导致 <option value="TYPE_CAMPAIGN">Campaign</option>

在 Contentful 中可以做类似的事情吗?

【问题讨论】:

    标签: contentful


    【解决方案1】:

    这里有内容的 DevRel。 ?

    对于短文本字段,我们不支持此功能。有两种方法可以解决这个用例。

    为其创建内容类型

    您可以创建一个单独的内容类型并以这种方式解决用例。然后,您可以引用这种分离的内容类型的条目,而不是使用短文本。您可以将您的枚举存储在那里,它还有一个优点是您可以在将来使用其他字段对其进行扩展。

    使用 App Framework(或 UI 扩展)扩展 Contentful 界面

    另一种选择是利用App Framework。该框架允许您用您的逻辑替换字段(条目甚至完整页面)。这样,您可以根据需要呈现带有标签和值的选择。

    【讨论】:

      【解决方案2】:
      Here is how I did it:
      
          const TestLevel = stationContentType.createField('testLevel', {
            name: 'TestLevel',
            type: 'Symbol',
            required: true,
            validations: [specificValueRestriction([TestLevels.testa, TestLevel.testb, TestLevel.testc])]
          });
      
      export enum TestLevels {
        testa = 'Test A',
        testb = 'Test B',
        testc = 'Test C',
      }
      
      export const specificValueRestriction = (specificValueRestriction: string[]): IValidation =>
      ({
        in: specificValueRestriction
      });
      
      And then I used datamappers to map the values to display the strings:
      
      function mapStaffingLevelEnumType(testLevel: string) {
        if (testLevel== 'testa')
          return TestLevels.testa;
        if (testLevel== 'testb')
          return TestLevels.testb;
        if (testLevel== 'testc')
          return TestLevels.testc;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-07
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多