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 13 posts 165 karma points
    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 3205 posts 10708 karma points MVP 4x 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 13 posts 165 karma points
    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 3205 posts 10708 karma points MVP 4x 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 13 posts 114 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 249 posts 1221 karma points
    26 days ago
    Sotiris Filippidis
    0

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

Please Sign in or register to post replies

Write your reply to:

Draft