在 Umbraco 13 中以编程方式添加块网格内容

问题描述 投票:0回答:1

我正在尝试为具有

pageBody
属性的节点创建一些块网格内容。

我通过使用内容服务来实现此功能,并且在大多数情况下运行良好。

我的结构如下所示:

    • 专栏
      • 编辑器
  • 专栏
    • 编辑器

本例中的编辑器是用于构建轮播的自定义文档类型。

为了创建结构和内容,我创建了一个遵循块网格内容结构的 JSON 对象,并且一切都运行良好,直到创建代表每张幻灯片的块列表项。

这是我用来创建块网格的 JSON:

{
  "layout": {
    "Umbraco.BlockGrid": [
      {
        "contentUdi": "umb://element/9b607641d0794014bd4624633bdf0bc1",
        "settingsUdi": "umb://element/94e04102756f469c820a596d6ec32ccd",
        "areas": [
          {
            "key": "6c8edc61-b52f-375b-b805-91746c66ce1c",
            "items": [
              {
                "contentUdi": "umb://element/34e47a1023404389bdfdc437f3e46038",
                "columnSpan": 0,
                "rowSpan": 0,
                "areas": [
                  {
                    "key": "469abcec-0c05-3e54-b0d5-6475308649d1",
                    "items": [
                      {
                        "contentUdi": "umb://element/9f80b0d085e84fcdaff056826a61c6fe",
                        "columnSpan": 6,
                        "rowSpan": 1,
                        "areas": [],
                        "settingsUdi": "umb://element/cee1cdd9cf9a4220851ca14f003f49a7"
                      }
                    ]
                  },
                  {
                    "key": "1dcb2bbb-d383-37a1-90fc-d5083d16778d",
                    "items": [
                      {
                        "contentUdi": "umb://element/94e844d9e67b482fbe5bc6a98f43fc86",
                        "columnSpan": 6,
                        "rowSpan": 1,
                        "areas": [],
                        "settingsUdi": "umb://element/ee9c940313e2457c9058d3bb62cc6284"
                      }
                    ]
                  }
                ],
                "settingsUdi": null
              }
            ]
          }
        ]
      }
    ]
  },
  "contentData": [
    {
      "contentTypeKey": "e09e94a3-3697-42c6-9a4d-eb8631e35f05",
      "udi": "umb://element/9f80b0d085e84fcdaff056826a61c6fe",
      "carouselSlides": {
        "layout": {
          "Umbraco.BlockGrid": [
            {
              "contentUdi": "umb://element/23851b764ede47cf84b536bd9fdc1c96"
            }
          ]
        },
        "contentData": [
          {
            "contentTypeKey": "0bb6d398-44cd-4699-81ee-1ee8fb2362a8",
            "udi": "umb://element/23851b764ede47cf84b536bd9fdc1c96",
            "carouselHeading": "Test",
            "carouselImage": "umb://media/515526e502d64d33bb3a3cf993d25b19"
          }
        ],
        "settingsData": []
      }
    },
    {
      "contentTypeKey": "65b260e3-2060-3376-9182-277a3566c721",
      "udi": "umb://element/77ebac535c9849f78b9d4f54fa058c61"
    },
    {
      "contentTypeKey": "e09e94a3-3697-42c6-9a4d-eb8631e35f05",
      "udi": "umb://element/94e844d9e67b482fbe5bc6a98f43fc86",
      "carouselSlides": {
        "layout": {
          "Umbraco.BlockGrid": [
            {
              "contentUdi": "umb://element/66244dfd6c7a40b0a2a634495ff0e754"
            }
          ]
        },
        "contentData": [
          {
            "contentTypeKey": "0bb6d398-44cd-4699-81ee-1ee8fb2362a8",
            "udi": "umb://element/66244dfd6c7a40b0a2a634495ff0e754",
            "carouselHeading": "Test",
            "carouselImage": "umb://media/515526e502d64d33bb3a3cf993d25b19"
          }
        ],
        "settingsData": []
      }
    }
  ],
  "settingsData": []
}

这将创建行、列和轮播,但

carouselSlides
属性为空。

为了进行比较,我尝试创建的幻灯片示例如下所示:

{
  "contentTypeKey": "e09e94a3-3697-42c6-9a4d-eb8631e35f05",
  "udi": "umb://element/9f80b0d085e84fcdaff056826a61c6fe",
  "carouselSlides": {
    "layout": {
      "Umbraco.BlockGrid": [
        {
          "contentUdi": "umb://element/23851b764ede47cf84b536bd9fdc1c96"
        }
      ]
    },
    "contentData": [
      {
        "contentTypeKey": "0bb6d398-44cd-4699-81ee-1ee8fb2362a8",
        "udi": "umb://element/23851b764ede47cf84b536bd9fdc1c96",
        "carouselHeading": "Test",
        "carouselImage": "umb://media/515526e502d64d33bb3a3cf993d25b19"
      }
    ],
    "settingsData": []
  }
}

如果我手动创建内容并取回数据,它会像这样返回:

{
  "contentTypeKey": "e09e94a3-3697-42c6-9a4d-eb8631e35f05",
  "udi": "umb://element/8d04bbe17cc84a6f8ec024a2cdf6e521",
  "displayControls": 0,
  "autoPlay": 1,
  "carouselSlides": {
    "layout": {
      "Umbraco.BlockList": [
        {
          "contentUdi": "umb://element/ccc99d993bfc4faea384eb03becb6b78"
        }
      ]
    },
    "contentData": [
      {
        "contentTypeKey": "0bb6d398-44cd-4699-81ee-1ee8fb2362a8",
        "udi": "umb://element/ccc99d993bfc4faea384eb03becb6b78",
        "carouselHeading": "test",
        "carouselImage": "umb://media/515526e502d64d33bb3a3cf993d25b19"
      }
    ],
    "settingsData": []
  }
}

这个结构看起来很匹配,但当我在 UI 中查看它时,它不会出现在内容上。

这就是我保存内容的方式:

string gridString = JsonSerializer.Serialize(newGrid, new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping });

node.SetValue("pageBody", gridString);

_contentService.SaveAndPublish(node);

没有错误,所有内容都会保存,但轮播幻灯片除外。

我在这上面花了很多时间,并且正在努力弄清楚发生了什么事,请让我摆脱痛苦!

谢谢,

json .net umbraco
1个回答
0
投票

我出去散步,突然意识到我指定了错误的数据类型,

Umbraco.BlockGrid
应该是
Umbraco.BlockList

非常愚蠢,但很高兴它已修复,这样我就可以继续前进!

© www.soinside.com 2019 - 2024. All rights reserved.