Created
October 30, 2014 05:07
-
-
Save RyanABailey/e170c36d2b42ca7f9c7a to your computer and use it in GitHub Desktop.
linkfield to url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Get the URL for a given sitecore linkfield | |
| /// </summary> | |
| /// <param name="lf">sitecore linkfield</param> | |
| /// <returns>web URL</returns> | |
| public static string GetLinkFieldURL(LinkField lf) | |
| { | |
| switch (lf.LinkType.ToLower()) | |
| { | |
| case "internal": | |
| // Use LinkMananger for internal links, if link is not empty | |
| return lf.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(lf.TargetItem) : string.Empty; | |
| case "media": | |
| // Use MediaManager for media links, if link is not empty | |
| return lf.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(lf.TargetItem) : string.Empty; | |
| case "external": | |
| // Just return external links | |
| return lf.Url; | |
| case "anchor": | |
| // Prefix anchor link with # if link if not empty | |
| return !string.IsNullOrEmpty(lf.Anchor) ? "#" + lf.Anchor : string.Empty; | |
| case "mailto": | |
| // Just return mailto link | |
| return lf.Url; | |
| case "javascript": | |
| // Just return javascript | |
| return lf.Url; | |
| default: | |
| // Just please the compiler, this | |
| // condition will never be met | |
| return lf.Url; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment