leekelleher
08/09/2023, 3:46 PMFallback
when I make calls to the content.Value("propertyAlias")
instead of having to write content.Value("propertyAlias", fallback: Fallback.ToDefaultLanguage)
every single time?
Of course, I could write a custom extension method to handle this, but wondering if it's possible out-of-the-box?D_Inventor
08/09/2023, 5:20 PMIPublishedValueFallback
. You can inject it with dependency injection and you pass it into the constructor of your content type. You can add a decorator onto that type and see if that works. Other than that, I haven't seen any options.leekelleher
08/10/2023, 10:15 AMIPublishedValueFallback
instance with my own?D_Inventor
08/10/2023, 10:23 AMcsharp
public class MyComposer : IComposer
{
public void Composer(IUmbracoBuilder builder)
{
builder.Services.Decorate<IPublishedValueFallback, DecoratorPublishedFallbackWithDefault>();
}
}
public class DecoratorPublishedFallbackWithDefault : IPublishedValueFallback
{
private readonly IPublishedValueFallback _decoratee;
public DecoratorPublishedFallbackWithDefault(IPublishedValueFallback decoratee)
{
_decoratee = decoratee;
}
// implement interface through _decoratee, but make changes as necessary (I can't remember the interface methods)
}
Or you can decorate it in a more localized way:
csharp
public class MyContentController : RenderController
{
private readonly IPublishedValueFallback _fallback;
public MyContentController(IPublishedValueFallback fallback)
{
_fallback = fallback;
}
public IActionResult Index()
{
var content = new MyContent(CurrentPage, new DecoratorPublishedFallbackWithDefault(_fallback));
return CurrentTemplate(content);
}
private sealed class DecoratorPublishedFallbackWithDefault : IPublishedValueFallback
{
private readonly IPublishedValueFallback _decoratee;
public DecoratorPublishedFallbackWithDefault(IPublishedValueFallback decoratee)
{
_decoratee = decoratee
}
// implement interface through _decoratee, but make changes as necessary (I can't remember the interface methods)
}
}
leekelleher
08/10/2023, 10:26 AM