Copied to clipboard

Flag this post as spam?

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


  • Jaffer 9 posts 110 karma points
    Jul 31, 2024 @ 13:42
    Jaffer
    0

    Umbraco 13 customize the generated models while generating models via Model builder

    Hello, I would like to add my extension method to the generated models using Model Builder. Is it possible in Umbraco 13?

    Example

    This is the default generated code for my property using Model Builder.

    public virtual string Testtest => this.Value(_publishedValueFallback, "testtest");

    But I want to generate the model property using Model Builder with my extension method like,

    public virtual string Testtest => this.GetValueWithLanguage(_publishedValueFallback, "testtest");

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2024 @ 19:32
    Huw Reddick
    0

    create a new partial class like below

    namespace Umbraco.Cms.Web.Common.PublishedModels
    {
        public partial class ModelToExtend
        {
    
            [ImplementPropertyType("Testtest")]
            public virtual string Testtest => this.GetValueWithLanguage(this._publishedValueFallback, "testtest");
        }
    }
    

    replace ModelToExtend with the class generated by modelsbuilder

  • Jaffer 9 posts 110 karma points
    Aug 01, 2024 @ 05:41
    Jaffer
    0

    Hello Huw Reddick,

    Thank you for your assistance.

    If I create a new partial class and replace it with a generated class, I lose my changes when generated the next time.

    I want to automatically replace this.Value with this.GetValueWithLanguage extension method whenever generating the class from the model builder.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 01, 2024 @ 12:40
    Huw Reddick
    0

    You are not replacing the generated class, the new partial class is to extend the generated partial class

  • Jaffer 9 posts 110 karma points
    Aug 06, 2024 @ 05:07
    Jaffer
    101

    I found the solution for customizing the generated models by using the 'IModelsGenerator' interface.

    We create our own 'CustomModelsGenerator.cs' class, which should be inherited with the 'IModelsGenerator' interface.

    https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/reference/templating/modelsbuilder/understand-and-extend#imodelsgenerator

    It is working as expected for my business needs.

Please Sign in or register to post replies

Write your reply to:

Draft