7

If I have a template that is inheriting a field from a base template, Is there any way to override the source of that field?

6

Branching off of @SzymonKuzniak's answer, the simple answer is "no" but the complex answer is "yes, with some trickery, by tapping into the getLookupSourceItems pipeline."

In order to minimize development cost and maximize your extensibility, maintainability, and reusability, my recommendation would be to write a processor for the getLookupSourceItems pipeline that executes "Source Query" (the name I would give to the Rule Context Folder) rules.

Benefits:

  1. You can create macros for common query patterns (e.g. Site Home, Site Globals, etc.)
  2. You can create far more powerful filters than those provided OOTB
  3. You can use conditions to make the rules template specific, or to make them apply to a template and all inherited templates
  4. Low implementation effort, with high extensibility, maintainability and flexibility

Words of caution:

  1. Don't make rules that filter the source based on the value of a field on the item (or even another item). If a change is made to the dependant field, the content author would need to save the item before the source filtering gets updated. Remember that a content author could have more than one window open too.
  2. If you make rules that affect an item template and all inherited templates than any "overriding rules" must come after the initial rule, or the condition will need to be updated in the original rule to exclude the specific inherited template(s).
|improve this answer|||||
2

I would say no. Templates inheritance differs from code inheritance. If you create a field of the same name in an inheriting template Sitecore would not interpret this correctly.

The easiest answer is: if the source is different then the field itself is different thus you have to define it in the template where you are able to provide correct source.

But...

you can use getLookupSourceItems pipeline to process the source of a base template and try to parametrize it there.

|improve this answer|||||
2

As of Sitecore 7.0 with the introduction of The Buckets DLL, the code:<type, assembly> Template Field DataSource option was introduced that allows you to inject code to process your Datasource option.

I've used this in the past to programmatically determine datasource values.

In addition, if you'd like to see a full implementation of a custom datasource implementation that utilizes similar functionality, you can look at my Value List Field.

using System;
using Sitecore.ContentSearch.Utilities;
using Sitecore.Diagnostics;

namespace Sitecore.SharedSource.ValueListField.Pipelines.GetLookupSourceValueLists
{
    internal class CustomCodeSource
    {
        [UsedImplicitly]
        public void Process(GetLookupSourceValueListsArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            if (!args.Source.StartsWith("code:")) return;

            var values = RunEnumeration(args.Source);
            if (values != null && values.Length > 0)
                args.Result.AddRange(values);
            args.AbortPipeline();
        }

        private static string[] RunEnumeration(string templateSource)
        {
            templateSource = templateSource.Replace("code:", string.Empty);
            var valueList = ReflectionUtility.CreateInstance(Type.GetType(templateSource)) as IValueList;
            return valueList == null ? new string[0] : valueList.ValueListQuery();
        }

    }
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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