Hmm.
I'd expect a fallback method to be initially available, I guess it's just as easy to use the non-modelsbuilder version when I need a fallback option.. Thanks though!
I guess it's just as easy to use the non-modelsbuilder version when I
need a fallback option..
I would recommend to tweak how the model is generated.
Assume that you need to implement a fall back value on a property. If you don't use the Modelsbuilder way you will need go update each time where you get the property in your code.
With Modelsbuilder tweaking you only need to change the generated model and can leave all your other code unchanged.
You can tweak the generated code perfectly, but is there a way to change the modelsbuilder behavior that it does that automatically,
i have changed
/// <summary>Static getter for Intro Description</summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
public static string GetIntroDescription(IIntroControls that) => that.Value<string>("introDescription");
into this manually
/// <summary>Static getter for Intro Description</summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
public static string GetIntroDescription(IIntroControls that) => that.Value<string>("introDescription", fallback: Fallback.ToLanguage);
but every time i generate the code it gets overwritten by the generated code, i know i can also use a property so it doesnt get updated anymore, but i want it to include that for each future property i change or add to a model.
Although it might not be the best solution, I managed to have ModelsBuilder properties bring the default language value with this little hack:
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Web.PublishedModels
{
public static class PublishedContentExtensions
{
public static T Value<T>(this IPublishedContent content, string alias, string culture = null, string segment = null, Fallback fallback = default, T defaultValue = default)
{
return Umbraco.Web.PublishedContentExtensions.Value<T>(content, alias, fallback: Fallback.ToLanguage);
}
}
}
The trick here is to use the Umbraco.Web.PublishedModels namespace (or your own namespace, if you're using a custom one) so that the generated files "see" this implementation of the static Value extension function first (as it's "closer" to it - a cruel way of overriding static functions, I know :) ) So what it does is that it forces fallback to the default language and calls the right extension function with that parameter. Tested it and it's working, although you may encounter ambiguous call errors if you're using Value elsewhere, which are easily fixable.
Best would be for Models Builder to generate models with fallback, of course, but until then...
Great solution. I have tried this and it worked as you have explained however I do have places where I cannot use strongly typed models and need to use .Value("") instead.
You said the following:
"you may encounter ambiguous call errors if you're using Value elsewhere, which are easily fixable."
I was struggling with this also. I have a way of working that solves my issue, but I need to implement it for each property which can be a hassle.
I'd much prefer to modify how the code is generated. Looking into this now.
Modelsbuilder fallback value ?
Hi,
When using Modelsbuilder, how do you use a fallback option?
Using regular items you can do
But using ModelsBuilder there seems to not be a method for this?
Hi Ambert,
For that to work you can tweak the models builder generation.
I talk a bit about this in this article : https://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/
This is V7, but the same applies to V8
Dave
Hmm. I'd expect a fallback method to be initially available, I guess it's just as easy to use the non-modelsbuilder version when I need a fallback option.. Thanks though!
I would recommend to tweak how the model is generated.
Assume that you need to implement a fall back value on a property. If you don't use the Modelsbuilder way you will need go update each time where you get the property in your code.
With Modelsbuilder tweaking you only need to change the generated model and can leave all your other code unchanged.
Dave
You can tweak the generated code perfectly, but is there a way to change the modelsbuilder behavior that it does that automatically,
i have changed
into this manually
but every time i generate the code it gets overwritten by the generated code, i know i can also use a property so it doesnt get updated anymore, but i want it to include that for each future property i change or add to a model.
How do i achieve this?
Although it might not be the best solution, I managed to have ModelsBuilder properties bring the default language value with this little hack:
The trick here is to use the Umbraco.Web.PublishedModels namespace (or your own namespace, if you're using a custom one) so that the generated files "see" this implementation of the static Value extension function first (as it's "closer" to it - a cruel way of overriding static functions, I know :) ) So what it does is that it forces fallback to the default language and calls the right extension function with that parameter. Tested it and it's working, although you may encounter ambiguous call errors if you're using Value elsewhere, which are easily fixable.
Best would be for Models Builder to generate models with fallback, of course, but until then...
Hi Sotiris,
Great solution. I have tried this and it worked as you have explained however I do have places where I cannot use strongly typed models and need to use .Value("") instead.
You said the following:
"you may encounter ambiguous call errors if you're using Value elsewhere, which are easily fixable."
Not sure how I avoid this when using .Value("")
How do you resolve the ambiguous calls?
Cheers,
Marc
I was struggling with this also. I have a way of working that solves my issue, but I need to implement it for each property which can be a hassle. I'd much prefer to modify how the code is generated. Looking into this now.
My current approach : http://jvantroyen.blogspot.com/2020/04/umbraco-8-modelsbuilder-and.html
is working on a reply...