Instantly share code, notes, and snippets.
Created
July 26, 2017 06:17
-
Save justsayantan/f854dce9266ab86696e12e64640f19bd to your computer and use it in GitHub Desktop.
Create Secondary Navigation From Structure Group
This file contains 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
public class CustomNavigationProvider : INavigationProvider | |
{ | |
// #region INavigationProvider Members | |
/// <summary> | |
/// Gets the Navigation Model (Sitemap) for a given Localization. | |
/// </summary> | |
/// <param name="localization">The Localization.</param> | |
/// <returns>The Navigation Model (Sitemap root Item).</returns> | |
public SitemapItem GetNavigationModel(Localization localization) | |
{ | |
using (new Tracer(localization)) | |
{ | |
string url = SiteConfiguration.LocalizeUrl("navigation.json", localization); | |
// TODO TSI-110: This is a temporary measure to cache the Navigation Model per request to not retrieve and serialize 3 times per request. Comprehensive caching strategy pending | |
string cacheKey = "navigation-" + url; | |
HttpContext httpContext = HttpContext.Current; | |
if (httpContext != null && httpContext.Items[cacheKey] != null) | |
{ | |
Log.Debug("Obtained Navigation Model from cache."); | |
return (SitemapItem)HttpContext.Current.Items[cacheKey]; | |
} | |
Log.Debug("Deserializing Navigation Model from raw content URL '{0}'", url); | |
DefaultProviderTmsc provider = new DefaultProviderTmsc(); | |
string navigationJsonString = provider.GetProperPageContent(url, localization); | |
SitemapItem result = JsonConvert.DeserializeObject<SitemapItem>(navigationJsonString); | |
if (httpContext != null) | |
{ | |
httpContext.Items[cacheKey] = result; | |
} | |
return result; | |
} | |
} | |
public SitemapItem GetContextSecondaryNavigationLinks(string requestUrlPath, Localization localization) | |
{ | |
using (new Tracer(requestUrlPath, localization)) | |
{ | |
SitemapItem sitemapItem = (SitemapItem)GetNavigationModel(localization); // Start with Sitemap root Item. | |
int levels = requestUrlPath.Split('/').Length; | |
SitemapItem secondaryNavigationModel = new SitemapItem(); | |
while (levels > 1 && sitemapItem.Items != null) | |
{ | |
sitemapItem = | |
sitemapItem.Items.FirstOrDefault( | |
i => requestUrlPath.StartsWith(i.Url, StringComparison.InvariantCultureIgnoreCase)); | |
if (sitemapItem != null) | |
{ | |
if (sitemapItem.Type != "Page") | |
{ | |
secondaryNavigationModel = sitemapItem; | |
} | |
levels--; | |
} | |
else | |
{ | |
break; | |
} | |
} | |
return secondaryNavigationModel; | |
} | |
} | |
/// <summary> | |
/// Gets Navigation Links for the top navigation menu for the given request URL path. | |
/// </summary> | |
/// <param name="requestUrlPath">The request URL path.</param> | |
/// <param name="localization">The Localization.</param> | |
/// <returns>The Navigation Links.</returns> | |
public NavigationLinks GetTopNavigationLinks(string requestUrlPath, Localization localization) | |
{ | |
using (new Tracer(requestUrlPath, localization)) | |
{ | |
NavigationLinks navigationLinks = new NavigationLinks(); | |
SitemapItem sitemapRoot = GetNavigationModel(localization); | |
foreach (SitemapItem item in sitemapRoot.Items.Where(i => i.Visible)) | |
{ | |
navigationLinks.Items.Add(CreateLink((item.Title == "Index") ? sitemapRoot : item)); | |
} | |
return navigationLinks; | |
} | |
} | |
/// <summary> | |
/// Gets Navigation Links for the context navigation panel for the given request URL path. | |
/// </summary> | |
/// <param name="requestUrlPath">The request URL path.</param> | |
/// <param name="localization">The Localization.</param> | |
/// <returns>The Navigation Links.</returns> | |
public NavigationLinks GetContextNavigationLinks(string requestUrlPath, Localization localization) | |
{ | |
using (new Tracer(requestUrlPath, localization)) | |
{ | |
NavigationLinks navigationLinks = new NavigationLinks(); | |
SitemapItem sitemapItem = GetNavigationModel(localization); // Start with Sitemap root Item. | |
int levels = requestUrlPath.Split('/').Length; | |
while (levels > 1 && sitemapItem.Items != null) | |
{ | |
SitemapItem newParent = sitemapItem.Items.FirstOrDefault(i => i.Type == "StructureGroup" && requestUrlPath.StartsWith(i.Url, StringComparison.InvariantCultureIgnoreCase)); | |
if (newParent == null) | |
{ | |
break; | |
} | |
sitemapItem = newParent; | |
} | |
if (sitemapItem != null && sitemapItem.Items != null) | |
{ | |
foreach (SitemapItem item in sitemapItem.Items.Where(i => i.Visible)) | |
{ | |
navigationLinks.Items.Add(CreateLink(item)); | |
} | |
} | |
return navigationLinks; | |
} | |
} | |
/// <summary> | |
/// Gets Navigation Links for the breadcrumb trail for the given request URL path. | |
/// </summary> | |
/// <param name="requestUrlPath">The request URL path.</param> | |
/// <param name="localization">The Localization.</param> | |
/// <returns>The Navigation Links.</returns> | |
public NavigationLinks GetBreadcrumbNavigationLinks(string requestUrlPath, Localization localization) | |
{ | |
using (new Tracer(requestUrlPath, localization)) | |
{ | |
NavigationLinks navigationLinks = new NavigationLinks(); | |
int levels = requestUrlPath.Split('/').Length; | |
SitemapItem sitemapItem = (SitemapItem)GetNavigationModel(localization); // Start with Sitemap root Item. | |
navigationLinks.Items.Add(CreateLink(sitemapItem)); | |
while (levels > 1 && sitemapItem.Items != null) | |
{ | |
sitemapItem = | |
sitemapItem.Items.FirstOrDefault( | |
i => requestUrlPath.StartsWith(i.Url, StringComparison.InvariantCultureIgnoreCase)); | |
if (sitemapItem != null) | |
{ | |
navigationLinks.Items.Add(CreateLink(sitemapItem)); | |
levels--; | |
} | |
else | |
{ | |
break; | |
} | |
} | |
return navigationLinks; | |
} | |
} | |
protected static Link CreateLink(SitemapItem sitemapItem) | |
{ | |
string url = sitemapItem.Url; | |
if (url.StartsWith("tcm:")) | |
{ | |
url = SiteConfiguration.LinkResolver.ResolveLink(url); | |
} | |
return new Link | |
{ | |
Url = url, | |
LinkText = sitemapItem.Title | |
}; | |
} | |
} |
This file contains 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
public class CustomNavigationsController : NavigationController | |
{ | |
public override ActionResult Navigation(EntityModel entity, string navType, int containerSize = 0) | |
{ | |
if (navType.Trim().ToLower() == "topmenu") | |
{ | |
SitemapItem model = (SitemapItem)SiteConfiguration.NavigationProvider.GetNavigationModel(WebRequestContext.Localization); | |
EntityModel sourceModel = (EnrichModel(entity) as EntityModel) ?? entity; | |
model.XpmMetadata = sourceModel.XpmMetadata; | |
model.XpmPropertyMetadata = sourceModel.XpmPropertyMetadata; | |
return View(sourceModel.MvcData.ViewName, model); | |
} | |
else if (navType.Trim().ToLower() == "secmenu") | |
{ | |
CustomNavigationProvider provider = new CustomNavigationProvider(); | |
SitemapItem model = (SitemapItem)provider.GetContextSecondaryNavigationLinks(Request.Path,WebRequestContext.Localization); | |
EntityModel sourceModel = (EnrichModel(entity) as EntityModel) ?? entity; | |
model.XpmMetadata = sourceModel.XpmMetadata; | |
model.XpmPropertyMetadata = sourceModel.XpmPropertyMetadata; | |
return View(sourceModel.MvcData.ViewName, model); | |
} | |
else | |
{ | |
return base.Navigation(entity, navType, containerSize); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment