Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ambert van Unen 175 posts 817 karma points c-trib
    Apr 11, 2019 @ 18:56
    Ambert van Unen
    0

    Modelsbuilder fallback value ?

    Hi,

    When using Modelsbuilder, how do you use a fallback option?

    Using regular items you can do

    Model.Value<string>("stringValue", fallback: Fallback.ToDefaultValue, defaultValue: "NothingFound");
    

    But using ModelsBuilder there seems to not be a method for this?

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 12, 2019 @ 06:11
    Dave Woestenborghs
    0

    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

  • Ambert van Unen 175 posts 817 karma points c-trib
    Apr 12, 2019 @ 06:37
    Ambert van Unen
    0

    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!

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 12, 2019 @ 07:14
    Dave Woestenborghs
    0

    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.

    Dave

  • Lander Debeuf 23 posts 125 karma points
    May 29, 2019 @ 09:15
    Lander Debeuf
    0

    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.

    How do i achieve this?

  • Sotiris Filippidis 286 posts 1501 karma points
    Aug 28, 2019 @ 22:46
    Sotiris Filippidis
    101

    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...

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Mar 01, 2020 @ 16:42
    Marc Love (uSkinned.net)
    1

    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

  • Jeroen Vantroyen 54 posts 394 karma points c-trib
    Apr 15, 2020 @ 11:58
    Jeroen Vantroyen
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft