public void ResolveLinkWithTemplateUri() { TridionLinkProvider.link2 = "/something"; var link = LinkFactory.ResolveLink("tcm:2-456-64", "tcm:2-123", "tcm:2-789-32"); Assert.IsNotNull(link); Assert.IsFalse(string.IsNullOrEmpty(link)); Assert.AreEqual(link, "/something"); }
public void ResolveLink() { TridionLinkProvider.link1 = "/something"; var link = LinkFactory.ResolveLink("tcm:2-123"); Assert.IsNotNull(link); Assert.IsFalse(string.IsNullOrEmpty(link)); Assert.AreEqual(link, "/something"); }
public void GetLinkFromCache() { TridionLinkProvider.link1 = "/something"; var link = LinkFactory.ResolveLink("tcm:2-123"); Assert.IsNotNull(link); Assert.IsFalse(string.IsNullOrEmpty(link)); TridionLinkProvider.link1 = "/something/else"; var newLink = LinkFactory.ResolveLink("tcm:2-123"); // Note: changing the underlying value of the link shouldn't matter because the LinkFactory should cache the link anyway Assert.AreEqual(link, newLink); }
/// <summary> /// Extension method on String to resolve rich text. /// Use as: Model.Field["key"].Value.ResolveRichText() /// </summary> /// <param name="value"></param> /// <returns>MvcHtmlString (resolved rich text)</returns> public static MvcHtmlString ResolveRichText(this String value) { LinkFactory linkFactory = new LinkFactory(); XmlDocument doc = new XmlDocument(); doc.LoadXml(string.Format("<xhtml>{0}</xhtml>", value)); XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml"); nsmgr.AddNamespace("xlink", "http://www.w3.org/1999/xlink"); // resolve links which haven't been resolved foreach (XmlNode link in doc.SelectNodes("//xhtml:a[@xlink:href[starts-with(string(.),'tcm:')]][@xhtml:href='' or not(@xhtml:href)]", nsmgr)) { string tcmuri = link.Attributes["xlink:href"].Value; string linkUrl = linkFactory.ResolveLink(tcmuri); if (!string.IsNullOrEmpty(linkUrl)) { // add href XmlAttribute href = doc.CreateAttribute("xhtml:href"); href.Value = linkUrl; link.Attributes.Append(href); // remove all xlink attributes foreach (XmlAttribute xlinkAttr in link.SelectNodes("//@xlink:*", nsmgr)) { link.Attributes.Remove(xlinkAttr); } } else { // copy child nodes of link so we keep them foreach (XmlNode child in link.ChildNodes) { link.ParentNode.InsertBefore(child.CloneNode(true), link); } // remove link node link.ParentNode.RemoveChild(link); } } return(new MvcHtmlString(doc.DocumentElement.InnerXml)); }
internal T BuildLink <T>(IFieldSet fields, T instance) where T : Link { //Func<string, string> FindInHierarchy = name => // fields // .Generate(fs => fs.Field("Link", fld => fld.EmbeddedValues.FirstOrDefault())) // .Select(fld => fld.Field(name)) // .LastOrDefault(s => !string.IsNullOrEmpty(s)); //var externalLink = FindInHierarchy("ExternalLink"); //var componentLink = FindInHierarchy("ComponentLink"); var externalLink = FindInHierarchy(fields, "ExternalLink"); var componentLink = FindInHierarchy(fields, "ComponentLink"); if (!string.IsNullOrEmpty(componentLink)) { componentLink = LinkFactory.ResolveLink(componentLink); } instance.Href = componentLink ?? externalLink; return(instance); }