Sitecore Asked on August 23, 2021
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
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();
}
Answered by Marek Musielak on August 23, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP