Copied to clipboard

Flag this post as spam?

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


  • Anders Brohus 194 posts 475 karma points
    Apr 12, 2019 @ 07:37
    Anders Brohus
    0

    Hi Our,

    Currently have an strange issue, because i can't extend my "NewsArticle" model, maybe it's me that is doing it wrong..

    Because i have an doc type named "News Article", which have inherited the doc type "News Meta", in the "News Meta" i have an heading field which i want to extend but when i regenerate my models it won't stop generating the heading field, which means i get this error,

    The type 'NewsArticle' already contains a definition for 'NewsHeading'

    The generated field looks like this,

        ///<summary>
        /// Overskrift
        ///</summary>
        [ImplementPropertyType("newsHeading")]
        public string NewsHeading
        {
            get { return ProjectName.Core.Models.NewsMeta.GetNewsHeading(this); }
        }
    

    And the code i have written looks like this,

    [ImplementPropertyType("newsHeading")]
        public string NewsHeading
        {
            get
            {
                string newsHeading = NewsMeta.GetNewsHeading(this);
                if (string.IsNullOrWhiteSpace(newsHeading))
                {
                    newsHeading = OpenGraphSharing.GetOgTitle(this);
    
                    if (string.IsNullOrWhiteSpace(newsHeading))
                    {
                        newsHeading = SEO.GetMetaTitle(this);
    
                        if (string.IsNullOrWhiteSpace(newsHeading))
                        {
                            newsHeading = this.Name;
                        }
                    }
                }
                return newsHeading;                  
            }
        }
    

    The funny part is i have done the same thing with many other thing which works, but they didn't have inherited doc types?

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

    Hi Andres,

    Looking at the code I think you are using a composition. Not inheritance? Is that correct.

    The news article generated class will probably implement a interface called INewsMeta.

    So if you go in the Newsmeta generated file you will see a interface INewsMeta and a class NewsMeta which implements the interface

    So what you want to do is create a partial class called NewsMeta

    In that you will add a property

     [ImplementPropertyType("newsHeading")]
        public string NewsHeading
        {
            get { return this.GetNewsHeading(this); }
        }
    

    And add a static method called GetNewsHeading

    public static string GetNewsHeading(INewsMeta that) {
    
    var newsHeading = that.GetPropertyValue<string>("newsHeading");
    
    if (string.IsNullOrWhiteSpace(newsHeading))
                {
                    newsHeading = OpenGraphSharing.GetOgTitle(that);
    
                    if (string.IsNullOrWhiteSpace(newsHeading))
                    {
                        newsHeading = SEO.GetMetaTitle(that);
    
                        if (string.IsNullOrWhiteSpace(newsHeading))
                        {
                            newsHeading = that.Name;
                        }
                    }
                }
                return newsHeading;   
    
     }
    

    After these changes regenerate your models.

    Dave

  • Anders Brohus 194 posts 475 karma points
    Apr 12, 2019 @ 10:55
    Anders Brohus
    0

    Hi Dave :)

    Of course i'm using composition but it's friday ;)

    Will try and see if this works, thanks alot! :)

  • Anders Brohus 194 posts 475 karma points
    Apr 12, 2019 @ 11:38
    Anders Brohus
    0

    Okay it's not working ..

    Now my NewsHeading has been removed from the generated NewsArticle .. :(

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

    Is the NewsArticle still implementing the INewsMeta interface ?

    What happens if you remove this from your own class

     [ImplementPropertyType("newsHeading")]
        public string NewsHeading
        {
            get { return this.GetNewsHeading(this); }
        }
    

    And only leave the GetNewsHeading

    Dave

  • Anders Brohus 194 posts 475 karma points
    Jul 05, 2019 @ 09:23
    Anders Brohus
    0

    Hi Dave :)

    I have a question related to this :)

    What about if i want to get the node's name in the model when i do the composition in Umbraco 8? :)

    Because i can't find the .Name on that? :)

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

    I just tested that on a project that is the trick.

    In your own partial class you only need to have the GetNewsHeading method.

    Than it will work.

    Dave

  • Anders Brohus 194 posts 475 karma points
    Apr 15, 2019 @ 09:35
    Anders Brohus
    0

    Nice thanks alot, it worked! :)

Please Sign in or register to post replies

Write your reply to:

Draft