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&_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&_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);
}