umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.5k stars 2.69k forks source link

Add Fallback.ToDefaultLanguage to get fallback value from the default language #8349

Closed ronaldbarendse closed 3 years ago

ronaldbarendse commented 4 years ago

As discussed in https://github.com/OurModelsBuilder/Our.ModelsBuilder/issues/206 and https://github.com/OurModelsBuilder/Our.ModelsBuilder/issues/235, the current options to get fallback values are quite minimal or very verbose. Some things can be fixed within Models Builder (e.g. generating additional extension methods accepting fallback options), customizing the model classes or even replacing the IPublishedValueFallback.

One thing that might be a good addition is adding Fallback.ToDefaultLanguage, so you can get a fallback value from the default language without configuring the fallback chain on the languages themselfs.

The default PublishedValueFallback implementation already depends on ILocalizationService, so getting the default langauge can be done using _localizationService.GetDefaultLanguageIsoCode(): https://github.com/umbraco/Umbraco-CMS/blob/51d7ce0dc6e7dbcfd0c85e3f6e863139fb545635/src/Umbraco.Web/Models/PublishedContent/PublishedValueFallback.cs#L21-L25

Looking at the code and comments in this class, it already requires some attention. Most of the logic can be moved into a base class, so replacing it with a custom implementation (e.g. using a custom language fallback chain or recurse ancestors & languages) can be written a lot easier!

umbrabot commented 3 years ago

Closing this in relation to #8350 - make sure to read the close reason on #8350