3

In our Sitecore instance we have a number of different languages, and we have enabled item level language fallback. We have enabled item level fallback on all of our page templates, and this works as intended.

The problem is that if a German version of a page not exists, we are correctly shown the English version of the page, but all of the components on the page are still using the German version of their datasources, which might not be defined.

As such, we would like to also enable item level language fallback on all datasources, preferably through templates and standard values such that we don't need to do it manually all the time. This is easily done for our own custom components through their datasource templates, but the issue is how to do it for ootb SXA components.

As I understand it, making changes to SXA templates is not only bad practice, the changes will also be overwritten the next time we upgrade SXA. I found this post on here, which talks about a way to modify an SXA datasource template through the use of datasource configurations, but doing this for every single SXA component seems a bit cumbersome.

Is this really the way to do it, or does someone perhaps have a good idea of a better way to enabled item level language fallback for all SXA datasources?

By the way, we are using Sitecore 9.1 with SXA 1.8.

1 Answer 1

4

I used the rules engine to fix this. I created a rule under Item Saved (/sitecore/system/Settings/Rules/Item Saved/Rules/Language Fallback) that sets the enable item fallback to true on every save.

It looks like this:

where the item is the my-root-item item or one of its descendants set __Enable item fallback value to 1

You can add any condition you want in the where clause - this one just takes all items under a parent item but you can do whatever needed there. You can also create multiple rules if needed.

This way I did not have to set this on standard values for any of the default SXA components, but all newly created items will get the fallback set.

If you already have items (and don't want to save them all), you could use SPE to set the value of __Enable item fallback to 1 on those existing items to start.

To save the field value in a rule, I added a custom action. Code looks like this:

public class SetFieldValueAction<T> : RuleAction<T> where T : RuleContext
{
    public ID FieldId { get; set; }

    public string FieldValue { get; set; }

    public override void Apply(T ruleContext)
    {
        var item = ruleContext.Item;
        if (item == null)
        {
            return;
        }

        var field = item.Fields[FieldId];
        if (field == null)
        {
            return;
        }

        if (field.Value.Equals(FieldValue))
        {
            return;
        }

        using (new EditContext(item))
        {
            item.Fields[FieldId].Value = FieldValue;
        }
    }
}
2
  • Looking in my Sitecore instance, I can create a rule with the same condition that you have used, but the only actions I can choose are to run a specific script or add a message to a log file, I can not set any values. I assume this is because it is an action I need to define myself? Jun 11, 2019 at 8:40
  • Right.. I created a custom action for that.. I'll add it to the answer.
    – Gatogordo
    Jun 11, 2019 at 9:05

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.