【问题标题】:How to link a droplink to a Treelist in Sitecore如何将下拉链接链接到 Sitecore 中的树列表
【发布时间】:2014-09-11 09:42:33
【问题描述】:
我试图弄清楚如何将Droplink 链接到Treelist 中的选定项目。
我有一个字段Theme,即Treelist,还有一个字段MasterTheme,即Droplink。
我应该能够在Droplink 中选择一个主主题,其中填充了来自Treelist 的选定数据。
我对 Sitecore 还很陌生,而且我不熟悉自定义类。
【问题讨论】:
标签:
sitecore
datasource
sitecore7
treelist
【解决方案1】:
您可以为此使用getLookupSourceItems-管道。使用Droplink,您可以将 Sitecore 查询指定为源。使用getLookupSourceItems,您可以在运行时更改源。以下处理器检查 Treelist 中选择的项目并创建一个 Sitecore 查询,其中包括 Treelist 中选择的所有项目。
public class LookupItemsFromField
{
private const string FromFieldParam = "fromfield";
public void Process(GetLookupSourceItemsArgs args)
{
// check if "fromfield" is available in the source
if (!args.Source.Contains(FromFieldParam))
{
return;
}
// get the field
var parameters = Sitecore.Web.WebUtil.ParseUrlParameters(args.Source);
var fieldName = parameters[FromFieldParam];
// set the source to a query with all items from the other field included
var items = args.Item[fieldName].Split('|');
args.Source = this.GetDataSource(items);
}
private string GetDataSource(IList<string> items)
{
if (!items.Any()) return string.Empty;
var query = items.Aggregate(string.Empty, (current, itemId) => current + string.Format(" or @@id='{0}'", itemId));
return string.Format("query://*[{0}]", query.Substring(" or ".Length));
}
}
您必须在Droplink 源中使用fromfield=<SourceField> 指定哪个字段是您的“源”字段:
最后你需要配置这个管道处理器:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<getLookupSourceItems>
<processor patch:before="processor[1]"
type="Website.LookupItemsFromField, Website" />
</getLookupSourceItems>
</pipelines>
</sitecore>
</configuration>