Instantly share code, notes, and snippets.
Created May 26, 2020
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
using System.Web.Configuration; | |
using Umbraco.Core; | |
using Umbraco.Web.Routing; | |
namespace Moriyama.Core.Routing.ContentFinders | |
{ | |
public class UmbracoHiddenFolderContentFinder : ContentFinderByNiceUrl | |
{ | |
public override bool TryFindContent(PublishedContentRequest contentRequest) | |
{ | |
var umbracoHiddenFolderAlias = WebConfigurationManager.AppSettings["UmbracoHiddenFolderAlias"] ?? "umbracoHiddenFolder"; | |
// We call the base content finder here which is ContentFinderByNiceUrl, | |
// that is why we remove ContentFinderByNiceUrl from the collection of content finders | |
if (base.TryFindContent(contentRequest)) | |
{ | |
// We see if we can find a node and if it is not a Hidden Folder then we return true | |
// We don't want to return a match for a folder by the normal nice URL | |
if (contentRequest.PublishedContent != null) | |
{ | |
return contentRequest.PublishedContent.ContentType.Alias != umbracoHiddenFolderAlias; | |
} | |
} | |
// Here we have to prepare the route | |
var path = contentRequest.Uri.GetAbsolutePathDecoded(); | |
string relativeRoute; | |
string route; | |
// if a domain has been set (multiple brands in one Umbraco - then routes have the id of that domain prepended to the path) | |
if (contentRequest.HasDomain) | |
{ | |
relativeRoute = DomainHelper.PathRelativeToDomain(contentRequest.DomainUri, path); | |
route = contentRequest.UmbracoDomain.RootContentId + "/" + relativeRoute.Replace("/_folder-", "/"); | |
} | |
else | |
{ | |
relativeRoute = path; | |
route = relativeRoute.Replace("/_folder-", "/"); | |
} | |
// Now we can try to find the folder after removing the _folder- prefix | |
var node = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(contentRequest.RoutingContext.UmbracoContext.InPreviewMode, route); | |
if (node != null && node.ContentType.Alias == umbracoHiddenFolderAlias) | |
{ | |
contentRequest.PublishedContent = node; | |
return true; | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment