0

so far I understand the richtextbox, when I insert a Internal sitecore link a href is created with link.aspx?_id=siteguid which is replaced in preview or published mode with the right url.

<a href="~/link.aspx?_id=1FC970F924A14C598445FFC0E099B55E&amp;_z=z">xyzlinkname</a>

I need to apply custom code for the link generation when the link is genrated for the preview or published mode. so the current link.aspx?_id=siteguid is perfekt. I need to extend the generated link which the link.aspx is creating in the preview mode for example.

The Experience Editor Edit Mode current Link is working that fine.

<a href="~/link.aspx?_id=1FC970F924A14C598445FFC0E099B55E&amp;_z=z">xyzlinkname</a>

Experience Editor Preview Mode or the Published page link will perhaps look like

<a href="/bluub/mysite" data-gtm="categorie">xyzlinkname</a>

and here it would be nice if I could add some Attributes. But Its more important for me to customize the href link "/bluub/mysite" for the selected guid from preview mode.

And here I thought there is a pipeline or resolver I can overwrite, when the link is generated from the link.aspx.

Update: With the help of Mark I've done the following.

some explanations first, we have a interface template on every page where the content manager can set the external url for this item, because its resolved by an proxy to an other location and in some time in the future we implement this page inside sitecore. When no external url is inserted then its resolved in sitecore. Inside my components we resolve this url with a custom link method to add some more content to the links like gtm attributes, ... and so I was looking for a solution to generate the links for the Rich Text fields.

So I've created a resolver like mark said with some custom logic I will post here

    public virtual void Process(RenderFieldArgs args)
    {
        Assert.ArgumentNotNull((object)args, "args");
        if (Context.PageMode.IsExperienceEditorEditing)
        {
            return;
        }

        if (args.FieldTypeKey == "rich text")
        {
            args.Result.FirstPart = Expand(args.Result.FirstPart, UrlOptions.DefaultOptions);
            args.Result.LastPart = Expand(args.Result.LastPart, UrlOptions.DefaultOptions);
        }
        else
        {
            //Original logic from the default processor
            args.Result.FirstPart = DynamicLink.ExpandLinks(args.Result.LastPart, Settings.Rendering.SiteResolving);
            args.Result.LastPart = DynamicLink.ExpandLinks(args.Result.LastPart, Settings.Rendering.SiteResolving);
        }
    }

I've extracted the expand method from sitecore from Sitecore.Links.ItemLinkExpander and just changed the return type from void to string

  public string Expand(string text, UrlOptions urlOptions)
    {
        Assert.ArgumentNotNull(text, "text");
        Assert.ArgumentNotNull(urlOptions, "urlOptions");
        int i = text.IndexOf("~/link.aspx?", StringComparison.InvariantCulture);
        if (i == -1)
        {
            return text;
        }
        StringBuilder stringBuilder = new StringBuilder(text.Length);
        int num = 0;
        while (i >= 0)
        {
            int num2 = text.IndexOf("_z=z", i, StringComparison.InvariantCulture);
            if (num2 < 0)
            {
                return stringBuilder.ToString();
            }
            string linkText = text.Substring(i, num2 - i);
            //ACHTUNG Hier haben wir einen Teil ersetzt und durch unsere Logik ausgetauscht die sich um die Linkerstellung kümmert!
            string url = GetRichTextLink(linkText, urlOptions);
            string value = text.Substring(num, i - num);
            stringBuilder.Append(value);
            stringBuilder.Append(url);
            num = num2 + "_z=z".Length;
            i = text.IndexOf("~/link.aspx?", num, StringComparison.InvariantCulture);
        }
        stringBuilder.Append(text.Substring(num));
        return stringBuilder.ToString();
    }

And I've added my custom code to extract the right Link from the selected item in the RTE

    public string GetRichTextLink(string linkText, UrlOptions urlOptions)
    {
        DynamicLink dynamicLink = DynamicLink.Parse(linkText);

        try
        {
            var linkItem = Sitecore.Context.Database.GetItem(dynamicLink.ItemId);
            if (linkItem.IsDerived(SitecoreExtensions.Templates.IstExternerLink.ID))
            {
                var sxpLink = linkItem.GetSxpLinkItem();
                return sxpLink.Link;
            }
        }
        catch (Exception) { }

        return dynamicLink.GetUrl(urlOptions);
    }
2

Whatever you're doing, I think you're off doing something pretty far removed from established Sitecore Best Practices. That said:

string richText = yourItem["rich text field"];
string expandedLink = LinkManager.ExpandDynamicLinks(richText);

Will do what you ask.

If you want to override Sitecore's own behaviour, you need to find the <renderField> pipeline and replace this processor with your own.

<processor type="Sitecore.Pipelines.RenderField.ExpandLinks, Sitecore.Kernel" />
  • Hi, I've adjusted my question a bit. I think you missunterstood my not so clear initial question or perhaps I missunderstood your answer :-) I don't want to change the original link.aspx?... I want to change the resulting published link generation. – squadwuschel May 5 '19 at 5:31
  • Updated answer. – Mark Cassidy May 5 '19 at 10:32
  • I've updated my question and added my solution to it. But one short question I've is this pipeline also the right place when I want to add some custom Attributes to my Link int the rich Text? – squadwuschel May 5 '19 at 17:58
  • That's one place to do it, yes – Mark Cassidy May 6 '19 at 7:55
0

to expand links in RTE's for Web visitors and not for experience editor mode: developers usually make use of PageMode.IsExperienceEditor, PageMode.IsPreview, PageMode.IsNormal to render markup. And use the above method mentioned by [Mark Cassidy] for IsNormal/IsPreview modes as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.