【问题标题】:Setting a Sitecore Template Field source value as a dynamic Sitecore query将 Sitecore 模板字段源值设置为动态 Sitecore 查询
【发布时间】:2014-04-07 20:34:27
【问题描述】:

我有一个用例,用户需要从项目下拉列表中选择一个字段值。这样做的问题是,下拉列表需要在每个项目(都具有相同的模板)上动态构建,以仅显示文件夹中字段值等于当前项目 ID 的项目。如果你已经迷路了,这里有一个结构示例:

- sitecore
  - content
    - Home
      - ContentItem1 (with droplist)
    - Site Data
      - SelectableItem1(ContentItem1 selected in 'itemid' field)
      - SelectableItem2(ContentItem1 selected in 'itemid' field)
      - SelectableItem3(ContentItem1 not selected in 'itemid' field)
      - SelectableItem4(ContentItem1 not selected in 'itemid' field)
  - templates
    - ContentItem1Template
      - Droplist field (source set to below query)

我希望我的查询动态分配 ContentItem1 的下拉列表字段源,方法是获取具有 ContentItem1 的 id 作为其“itemid”字段值的项目列表,但通过将该字段值与 ContentItem1 id 的值进行比较。我已经尝试通过将字段的值与 id 令牌进行比较来做到这一点,如下所示:

query:/sitecore/content/Site Data/*[@#itemid#=$id]

无论我为 id 尝试什么值('$id'、$id、@id、'@id'、@@id、'@@id' 等),它都不想在项目上解析等级。有没有办法做到这一点,以便我可以将这个 ContentItem1Template 重用于需要相同功能的所有项目?

【问题讨论】:

    标签: templates dynamic field sitecore


    【解决方案1】:

    如果您使用的是 Sitecore 7,那么您可以使用编码字段数据源。这将允许您使用任何您喜欢的自定义逻辑来指定应出现在列表中的项目。

    创建一个实现IDataSourceListQuery() 方法的类,该方法返回一个项目列表作为字段的来源。然后使用 code: 前缀将字段的源设置为您的方法,例如code:MyProject.Custom.FieldDataSource,MyProject.Custom

    using System;
    using Sitecore.Buckets.FieldTypes;
    using Sitecore.Data.Items;
    
    namespace MyProject.Custom
    {
      public class FieldDataSource : IDataSource
      {
        public Item[] ListQuery(Item item)
        {
          var root = item.Database.GetItem("/sitecore/content/my-item");
          // some other logic to filter your item set
          return root.Children.ToArray();
        }
      }
    }
    

    这些文章应该对您有所帮助:

    【讨论】:

      【解决方案2】:

      您可能需要将 ID 用单引号括起来,如下所示:

      query:/sitecore/content/#Site Data#/*[@itemid='$id']
      

      也就是说,这似乎非常适合使用 Sitecore 链接数据库。每当您将 SelectableItem 与 ContentItem 关联时,Sitecore 都会将该关系存储在 Link 数据库中(只要您使用支持它的字段引用它,例如 DropLink、DropTree、GeneralLink 等)。

      从那里,您可以使用Globals.LinkDatabase.GetReferrers(contentItem)contentItem.Links.GetValidLinks() 获取指向内容项的所有引用项的列表。您可以在此处按模板 ID 过滤列表,以确保仅返回 SelectableItems。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多