3

I have a source query over a standard Multi-List field and I want the query to retrieve items based on the current selected language.

Is there any attribute that could handle that in the source query?

6
  • Hi Tareq - welcome to SSE. Is that a standard Multilist or a Multilist with Search ?
    – Richard Seal
    Sep 11, 2018 at 11:40
  • @RichardSeal standard Multilist Sep 11, 2018 at 12:29
  • sitecore.stackexchange.com/questions/13587/…. You can use custom token to change the source Sep 11, 2018 at 13:29
  • @HishaamNamooya This could be very useful, I'm trying now Sep 11, 2018 at 14:20
  • @HishaamNamooya What I want is that I have multilist that shows all items to select from, however I need to show only items that has a version of the currently selected language, at first I thought this could be done by the source query however there is no @@language attribute, do you have any idea? Sep 11, 2018 at 15:14

1 Answer 1

3

OOTB Sitecore, this is not possible using Sitecore queries because Sitecore does not take into account which language has been selected.

Get Lookup Source Item

You can use the Lookup Source Item pipeline (<getLookupSourceItems>). This particular pipeline is responsible to execute the Sitecore query you've filled in the Datasource Field. Moreover, you can implement your business logic to retrieve items.

Implementation

You will need to implement a method which has GetLookupSourceItemsArgs as parameter. The args paramter has a property named Item which contain the selected item in the current selected version.

public void Process(GetLookupSourceItemsArgs args)
{
    if (this.SkipProcessor(args)) return;

    var items = Factory.GetDatabase("master").GetItem("PATH TO CONTAINER ITEM OR SITECORE ID"), args.Item.Language).GetChildren(ChildListOptions.SkipSorting);

    foreach (var item in items)
    {
        var itemVersions = item.Versions.GetVersions();

        foreach (var itemVersion in itemVersions)
        {
            if (itemVersion.Language.Name.Equals(args.Item.Language.Name))
            {
                args.Result.Add(itemVersion);
            }
        }
    }

    args.AbortPipeline();
}

private bool SkipProcessor(GetLookupSourceItemsArgs args)
{
    return !args.Source.ToLower().Contains("itemversionspecific");
}

Configuration File

Once you implemented the code, you need to add it to the getLookupSourceItems pipeline. Below is an example of the patch.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getLookupSourceItems>
        <processor type="YourNamespace,YourAssembly" patch:before="*"/>
      </getLookupSourceItems>
    </pipelines>
  </sitecore>
</configuration>

Testing

Based on the above code, I tested it by creating a container item named Items holding 3 items

  1. Item with English version only
  2. Item with French version only
  3. Item with both English and French version

English Version Selected enter image description here

French Version Selected enter image description here

Upon selection of a language version, the items from the Multi-List Field changes the list of items.

1
  • Perfect, thank you @Hishaam you are awesome :) Sep 12, 2018 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.