How to link a droplink to a Treelist in Sitecore

2020-03-27 08:36发布

问题:

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 Treelist, and a field MasterTheme, which is the Droplink.

I should be able to select a master-theme in the Droplink, which is filled with the selected data from the Treelist.

I'm pretty new to Sitecore, and I'm not familiar with Custom classes.

回答1:

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<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));
    }
}

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

At the end you need to configure this pipeline processor:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getLookupSourceItems>
        <processor  patch:before="processor[1]"
                    type="Website.LookupItemsFromField, Website" />
      </getLookupSourceItems>
    </pipelines>
  </sitecore>
</configuration>


回答2:

I think this is what you are looking for: http://getfishtank.ca/blog/using-item-field-as-a-data-source-in-sitecore

Basically you will be able to set the datasource of one field to another.