Copied to clipboard

Flag this post as spam?

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


  • David 11 posts 143 karma points
    Dec 11, 2018 @ 14:53
    David
    0

    Get Document Type Of Internal Related Article

    I have an article document type and in that I have a section for adding related links to other articles. In the related articles section I can add links to an external site by just providing the URL or I can add a link to an internal article I wrote.

    When retrieving the related articles I need to be able to determine if this is one of my internal articles or a link to another site. The best way to do this would be to just grab the document type, then do a switch on that.

    How would I go about getting the document type of a property within another document type?

    Here is my foreach I'm using to iterate through the related articles:

    @foreach (var article in CurrentPage.relatedArticles)
    {
        // render html based on articles document type
    }
    

    Not sure if I'm able to even get the document type of article as I can't see that property anywhere in the debugger.

  • Laura Weatherhead 25 posts 153 karma points MVP 6x c-trib
    Dec 11, 2018 @ 15:00
    Laura Weatherhead
    1

    Hey David,

    What datatype are you using to store the related articles?

    Is it the built-in picker or something like https://our.umbraco.com/packages/backoffice-extensions/multi-url-picker/ ?

    The RJP MultiUrlPicker surfaces a property indicating what type of link has been added, which might be helpful for you :)

    Cheers,

    Laura

  • David 11 posts 143 karma points
    Dec 11, 2018 @ 15:59
    David
    0

    Hey Laura,

    I'm actually using the obsolete Related Links data type (https://our.umbraco.com/documentation/getting-started/backoffice/property-editors/built-in-property-editors/related-links)

    It's an older site and there's a lot of document types that are using this data type so unfortunately I don't think I can switch :(.

    Best,

    David

  • Laura Weatherhead 25 posts 153 karma points MVP 6x c-trib
    Dec 11, 2018 @ 16:18
    Laura Weatherhead
    2

    Hey David,

    Looks like you might still be able to get the link type using one of the snippets posted on that page as a base:

    @using Newtonsoft.Json.Linq 
    @{      
        if (Model.Content.HasValue("relatedLinks") && Model.Content.GetPropertyValue<string>("relatedLinks").Length > 2)
        {
            <ul>
                @foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks"))
                {
                    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                    var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
                    <li><a href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption"))</a></li>
                }
            </ul>
        }
    }
    

    The helpful bit being

    item.Value<bool>("isInternal")
    

    If it is, then you can use the id to search for the content node, if it's not then it's presumably an external link.

    Cheers,

    Laura

  • David 11 posts 143 karma points
    Dec 11, 2018 @ 17:30
    David
    101

    Hey Laura,

    I forgot to explain that some of my "internal" articles are actually just links to external articles that are related to my articles. I just set them up as internal articles so I can store meta data I get from the external article in my database. So they are all considered "internal" as far as Umbraco is concerned. This means that the link generated by Umbraco for that node goes nowhere.

    What I ended up doing was taking the internal id in my foreach loop and using Umbraco.Content(internalId) to get the External Article node. If it actually is a node and not just a link then the id is greater than 0. After that I could get the DocumentTypeAlias and perform my if statement.

    It's kind of a mess but it works.

    Thanks for the help Laura!

    Code for anyone interested:

    var internalId = item.Value<int>("internal");
    MyMetaInformationModel externalNewsArticle = null;
    if (internalId > 0)
    {
        var node = Umbraco.Content(internalId);
        if (node.DocumentTypeAlias == "externalArticleNews")
        {
            externalNewsArticle = JsonConvert.DeserializeObject<MyMetaInformationModel>(node.GetPropertyValue<string>("externalUrl"));
        }
    }
    
    if (externalNewsArticle != null)
    {
        var linkUrl = externalNewsArticle.Url;
        // got the external url YAY!
    }
    else
    {
        // just a regular article
    }
    
  • Laura Weatherhead 25 posts 153 karma points MVP 6x c-trib
    Dec 11, 2018 @ 17:42
    Laura Weatherhead
    1

    Awesome :) glad you got it working!

Please Sign in or register to post replies

Write your reply to:

Draft