Copied to clipboard

Flag this post as spam?

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


  • Patrick Shaun McNicholas 24 posts 206 karma points
    Jun 10, 2021 @ 05:58
    Patrick Shaun McNicholas
    0

    Replace link URL in Rich Text editor in a grid with umbracoUrlAlias

    I'm using grid layouts and a custom folder structure to keep content organized in my site structure.

    So I have the pages in my footer organized in a folder called Footer Links In there I have children like About Us and Contact Us

    So the default page Urls would be /footer-links/about-us and /footer-links/contact-us.

    However these pages also have umbracoUrlAlias properties on them so that the final Url of the document is just /about-us and /contact-us

    I would like to be able to use the rich text editor and the link property in that editor and render the page with the umbracoUrlAlias property instead of the default behavior of using the normal long Url property of those links.

    Is there an easy way to do this without having to modify the default grid and rte property editors and views?

    I see within the actual JSON object saved in the grid those links come in using the UDI values, but how can I render the final grid using the umbracoUrlAlias instead of its default behavior?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Jun 10, 2021 @ 06:37
    Huw Reddick
    0

    You should be able to just type in the URL rather than selecting a content node

  • Patrick Shaun McNicholas 24 posts 206 karma points
    Jun 10, 2021 @ 08:29
    Patrick Shaun McNicholas
    100

    @Huw Reddick That defeats the purpose of the concept of dynamic content and node structure. I want this to be dynamic so that it moves if someone else moves the document somewhere else in the site structure.

    After several hours of research and experimentation here's what I came up with...

    This replaces the /Views/Partials/Grid/Editors/Rte.cshtml

    @model dynamic
    @using Umbraco.Web.Composing
    @using Umbraco.Web.Templates
    @{
        string currentRTEString = Model.value.ToString();
        string localLink = "/{localLink:"; 
        var value = Model.value.ToString();
        value = TemplateUtilities.ResolveUrlsFromTextString(value);
        value = TemplateUtilities.ResolveMediaFromTextString(value);
        if(currentRTEString.Contains(localLink)){
            int index = 0;
            while ((index=currentRTEString.IndexOf(localLink, index)) != -1)
            {
                string thisElement = currentRTEString.Substring((index), (currentRTEString.IndexOf("}\"",index) - index + 1));
                string udiAsString = thisElement.Replace("/{localLink:","").Replace("}","");
                var helper = Umbraco.Web.Composing.Current.UmbracoHelper;
                var node = helper.Content(udiAsString);
                string newUrl = node != null && node.HasValue("umbracoUrlAlias") ? node.Value<string>("umbracoUrlAlias") : 
                    node != null ? node.Url : "#";
                value = value.Replace(thisElement, newUrl);
                index++;
            }
        }
    }
    @Html.Raw(value)
    
  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Jun 10, 2021 @ 09:14
    Huw Reddick
    0

    surely the point of the umbracoUrlAlias is that it doesn't matter if they move the content! the umbracoUrlAlias does not change only the nodes url changes

  • Patrick Shaun McNicholas 24 posts 206 karma points
    Jun 10, 2021 @ 09:27
    Patrick Shaun McNicholas
    1

    Sure... but experience tells us that we need to idiot proof these kinds of situations. Since the Link is a fixed content picker how do you explain to inexperienced people the difference between when they should use the picker or type in the url. Both would work in this instance regardless if they use the picker or type the url. Otherwise some editors will do it correctly and some will not. So I just wanted the ability to use the alias if it's available. It's just a safeguard. Since we have so many people editing the site and employee turn around etc...

    But yes your original suggestion is viable, just not ideal in my situation.

  • Patrick Shaun McNicholas 24 posts 206 karma points
    Jun 10, 2021 @ 09:33
    Patrick Shaun McNicholas
    0

    Also editors can break the hand coded link by someone accidentally removing the alias from the node in the future. This ensures that if it exists it will use the alias, and if it doesn't exist it will use the regular url, and both instances will dynamically be linked to the node based on its ID and not base on someone's potentially miss-typed url string.

Please Sign in or register to post replies

Write your reply to:

Draft