Probably could use a lot of improvement:) But started working with Autofolders for a blog-type kind of functionality and found out that the current version of Autofolders isn't working as expected any more:( Besides that i really like the idea and approach.
But I started working on the 2.0.3 source code from http://autofolders.codeplex.com/
Challenge 1: NiceUrl()
The Niceurl() of autofolders function looks if truncatedFolderUrl is provided on FolderProvider Level, but the Autofoldersettings looks for it at AutoFolder Level. Nice would be if FolderProvider knew what AutoFolders invoked the call. But for now quick fix, pass along a boolean doTruncateFolders to the NiceUrl function in XsltExtensionLibrary
Changes in BaseFolderProvider.cs:
public virtual string NiceUrl(string realUrl) { return NiceUrl(realUrl, false); } public virtual string NiceUrl(string realUrl, bool doTruncatedFolder) { if (doTruncatedFolder) { int additionFolders = this.CreatedFolderDepth; List<string> urlParts = new List<string>(realUrl.Split('/')); if (urlParts.Count > (additionFolders + 1)) { while (additionFolders > 0) { urlParts.RemoveAt(urlParts.Count - 2); additionFolders--; } } string truncatedUrl = ""; foreach (string part in urlParts) { if (truncatedUrl.Length > 0) truncatedUrl += "/"; truncatedUrl += part; } return "/" + truncatedUrl; } else { return realUrl; } }
change to IFolderProvider
string NiceUrl(string url); string NiceUrl(string url, bool truncatedFolderUrl);
change to XsltExtensionLibrary.cs
if (autoFolderSetting != null) { //property TruncatedFolderUrl of FolderProvider isn't set anywhere, but isn't used either so call to function with a boolean to truncate url if(!String.IsNullOrEmpty(autoFolderSetting.TruncatedFolderUrl)) return autoFolderSetting.FolderProvider.NiceUrl(umbraco.library.NiceUrl(pageId), true); else return autoFolderSetting.FolderProvider.NiceUrl(umbraco.library.NiceUrl(pageId), false); }
Challenge 2 : NotFoundHandler for the internal redirect
This is in no way backwards compatible and can only be used for 4.11.1.
Changes in NotFoundHandler.cs
1. Get the type of the node
2. Add functionality to tell NiceUrl() to truncate the url
public static string NiceUrl(int pageId) { AutoFolderSetting autoFolderSetting = null; XPathNodeIterator pageNode = umbraco.library.GetXmlNodeById(pageId.ToString()); if (pageNode != null) { //string docType = pageNode.Current.GetAttribute("nodeTypeAlias", ""); //changed becasue .GetAttribute("nodeTypeAlias", ""); returns null string docType = pageNode.Current.LocalName; autoFolderSetting = AutoFolderSettings.Instance.GetSettings(docType); } if (autoFolderSetting != null) { //property TruncatedFolderUrl of FolderProvider isn't set anywhere, but isn't used either so call to function with a boolean to truncate url if(!String.IsNullOrEmpty(autoFolderSetting.TruncatedFolderUrl)) return autoFolderSetting.FolderProvider.NiceUrl(umbraco.library.NiceUrl(pageId), true); else return autoFolderSetting.FolderProvider.NiceUrl(umbraco.library.NiceUrl(pageId), false); } else { return umbraco.library.NiceUrl(pageId); } }