How to link a droplink to a Treelist in Sitecore

后端 未结 2 543
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 01:31

I\'m trying to figure out how I can link a Droplink to the selected items in a Treelist. I have a field Theme, which is the Tre

2条回答
  •  孤街浪徒
    2021-01-15 02:30

    You can use the getLookupSourceItems-pipeline for this. With a Droplink you can specify a Sitecore query as source. And with the getLookupSourceItems you can change the source at runtime. The following processor checks the items selected in the Treelist and create a Sitecore query which includes all the items selected in the 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 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));
        }
    }
    

    You have to specify which field is your "Source" field within the Droplink source with fromfield=:

    enter image description here

    At the end you need to configure this pipeline processor:

    
      
        
          
            
          
        
      
    
    

提交回复
热议问题