2

I want to redirect my urls added in the Rich Text Editor,General Links to custom urls on page load.

To get the URLS which exist in the General Link field and in the Rich Text Editor, I am using the below code:

public override string GetItemUrl(Item item, ItemUrlBuilderOptions options)
        {
        
            var url = base.GetItemUrl(item, options)
            return url;
         }

Please provide a suggestion how to get URLS added in the General Field and Rich Text editor on the link manager. Thanks

1 Answer 1

4

From what I understand, you want to implement custom logic for generation of internal links added by content authors in Rich Text fields.

If that's the case, you need to change the logic of renderField pipeline and its ExpandLinks processor.

By default this processor calls DynamicLink.ExpandLinks(...) method.

You would need to replace that with your custom logic. Write a class like that:

  public class CustomExpandLinks
  {
    public virtual void Process(RenderFieldArgs args)
    {
      Assert.ArgumentNotNull((object) args, nameof (args));
      if (Context.PageMode.IsExperienceEditorEditing)
        return;
      args.Result.FirstPart = ExpandUrlsInCustomWay(args.Result.FirstPart, Settings.Rendering.SiteResolving);
      args.Result.LastPart = ExpandUrlsInCustomWay(args.Result.LastPart, Settings.Rendering.SiteResolving);
    }
  }

and register it instead of default one (or maybe before if you need a fallback):

<configuration xmlns:patch="https://www.sitecore.com/xmlconfig/">
    <sitecore>
        <pipelines>
            <renderField>
                <processor
                    type="MyAssembly.MyNamespace.CustomExpandLinks, MyAssembly"
                    patch:after="processor[@type='Sitecore.Pipelines.RenderField.ExpandLinks, Sitecore.Kernel']" />
            </renderField>
        </pipelines>
    </sitecore>
</configuration>

Be aware that this pipeline is called for every type of field and that args.Result.FirstPart and args.Result.LastPart contain the whole content of Rich Text field value, so you will have to parse the links from it on your own.

Below is code which is not tested, but should be a good start for implementation of custom logic of expanding dynamic urls from Rich Text fields:

public string ExpandUrlsInCustomWay(string text, UrlOptions urlOptions)
{
    int startIndex1 = text.IndexOf("~/link.aspx?", StringComparison.InvariantCulture);

    if (startIndex1 == -1)
        return text;

    StringBuilder stringBuilder = new StringBuilder(text.Length);
    int startIndex2 = 0;
    for (; startIndex1 >= 0; startIndex1 = text.IndexOf("~/link.aspx?", startIndex2, StringComparison.InvariantCulture))
    {
        int num = text.IndexOf("_z=z", startIndex1, StringComparison.InvariantCulture);
        if (num < 0)
        {
            text = stringBuilder.ToString();
            return text;
        }

        var dynamicLink = DynamicLink.Parse(text.Substring(startIndex1, num - startIndex1));
        // Sitecore does something like the line below. You need to replace it with custom code
        
        string url = dynamicLink.GetUrl(urlOptions); #to be removed
        
        // DynamicLink class contains ItemId, Language, Text and others.

        var item = (dynamicLink.Site ?? Context.Site)
            .Database.GetItem(dynamicLink.ItemId, dynamicLink.Language);

        url = GetUrlInSomeCustomWay(item);
        
        string str = text.Substring(startIndex2, startIndex1 - startIndex2);
        stringBuilder.Append(str);
        stringBuilder.Append(url);
        startIndex2 = num + "_z=z".Length;
    }
    stringBuilder.Append(text.Substring(startIndex2));
    return stringBuilder.ToString();
}
4
  • Why are you adding different types of data type to the function ExpandUrlsInCustomWay()? @Marek Musielak Commented Jul 17, 2020 at 6:56
  • I just checked what Sitecore does and used the same logic. You can write things in your own way if you want
    – Marek Musielak
    Commented Jul 17, 2020 at 6:59
  • I am not able to get the General Link in the RenderFieldArgs args @Marek Musielak Commented Jul 17, 2020 at 9:40
  • I am unable to get the General Link field while calling the layout service api, otherwise it is working. pls help. Commented Jul 20, 2020 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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