Created
June 1, 2015 15:44
-
-
Save tomhawkin/e151e6600bdb766f40fc to your computer and use it in GitHub Desktop.
A modified GetDatasourceLocationWithQuery class to enable Sitecore components to have datasources with relative paths to a folder in sitecore and a modified TreelistWithRelativePathsAndQuery class to enable relative paths in tree lists
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> | |
| // adds the the default query functionality to also get items under the current item by using './' | |
| // or gets items relative to the root of the current site with 'fromroot: | |
| // </summary> | |
| // -------------------------------------------------------------------------------------------------------------------- | |
| namespace Site.Website.Processors | |
| { | |
| using System.Linq; | |
| using Sitecore; | |
| using Sitecore.Data.Items; | |
| using Sitecore.Diagnostics; | |
| using Sitecore.Pipelines.GetRenderingDatasource; | |
| using Sitecore.Text; | |
| using Sitecore.Web; | |
| /// <summary> | |
| /// The get datasource location with query. | |
| /// </summary> | |
| public class GetDatasourceLocationWithQuery | |
| { | |
| /// <summary> | |
| /// Processes the specified args. | |
| /// </summary> | |
| /// <param name="args"> | |
| /// The args. | |
| /// </param> | |
| public void Process(GetRenderingDatasourceArgs args) | |
| { | |
| Assert.IsNotNull(args, "args"); | |
| var dataSources = new ListString(args.RenderingItem["Datasource Location"]); | |
| // the input Datasource Location may contain a few sources which are separated by the “|”, so str now is a list of these sources | |
| foreach (var dataSource in dataSources) | |
| { | |
| string path; | |
| Item item = null; | |
| // relative path for items below the current one | |
| if (dataSource.StartsWith("./") && !string.IsNullOrEmpty(args.ContextItemPath)) | |
| { | |
| path = args.ContextItemPath + dataSource.Remove(0, 1); | |
| item = args.ContentDatabase.GetItem(path); | |
| } | |
| // relative path from the root of the current site | |
| if (dataSource.StartsWith("fromroot:")) | |
| { | |
| var contextItem = args.ContentDatabase.GetItem(args.ContextItemPath); | |
| var site = GetSite(contextItem); | |
| var db = Sitecore.Configuration.Factory.GetDatabase("master"); | |
| var rootItem = db.Items[site.RootPath]; | |
| path = rootItem.Paths.FullPath + dataSource.Substring("fromroot:".Length); | |
| item = args.ContentDatabase.GetItem(path); | |
| } | |
| // standard query | |
| if (dataSource.StartsWith("query:")) | |
| { | |
| var startItemPath = Context.Site.StartPath.TrimEnd('/'); | |
| if (!string.IsNullOrEmpty(startItemPath)) | |
| { | |
| var i = Context.Database.GetItem(startItemPath); | |
| if (i != null) | |
| { | |
| var itemQueried = i.Axes.SelectItems(dataSource.Substring("query:".Length)); | |
| if (itemQueried != null) | |
| { | |
| item = itemQueried.Last(); | |
| } | |
| } | |
| } | |
| } | |
| if (item != null) | |
| { | |
| args.DatasourceRoots.Add(item); | |
| } | |
| } | |
| } | |
| /// <summary> | |
| /// Gets the current site from the item path or template path | |
| /// </summary> | |
| /// <param name="item"> | |
| /// The item. | |
| /// </param> | |
| /// <returns> | |
| /// The <see cref="SiteInfo"/>. | |
| /// </returns> | |
| private static SiteInfo GetSite(Item item) | |
| { | |
| var siteInfoList = Sitecore.Configuration.Factory.GetSiteInfoList(); | |
| foreach (var siteInfo in siteInfoList) | |
| { | |
| if (item.Paths.FullPath.ToLower().Contains("/sitecore/content/" + siteInfo.Name.ToLower()) || item.Paths.FullPath.ToLower().Contains("/sitecore/templates/user defined/" + siteInfo.Name.ToLower())) | |
| { | |
| return siteInfo; | |
| } | |
| } | |
| return siteInfoList.First(n => n.Name.ToLower().Contains("website")); | |
| } | |
| } | |
| } |
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> | |
| // The treelist with relative paths and query. | |
| // To enable this, go to /sitecore/system/Field types/List Types/Treelist in the core database | |
| // clear out the 'control' field and fill in the assembly and class fields with the values for this class. | |
| // currently that is 'Site.Website' and 'Site.Website.TreelistWithRelativePathsAndQuery' | |
| // </summary> | |
| // -------------------------------------------------------------------------------------------------------------------- | |
| namespace Site.Website.Processors | |
| { | |
| using System; | |
| using System.Linq; | |
| using Site.Core.SitecoreHelpers; | |
| using Sitecore.Data.Items; | |
| using Sitecore.Diagnostics; | |
| using Sitecore.Globalization; | |
| using Sitecore.Shell.Applications.ContentEditor; | |
| using Sitecore.Web; | |
| using Version = Sitecore.Data.Version; | |
| /// <summary> | |
| /// The treelist with relative paths and query. | |
| /// </summary> | |
| public class TreelistWithRelativePathsAndQuery : TreeList | |
| { | |
| /// <summary> | |
| /// The _current item. | |
| /// </summary> | |
| private Item currentItem; | |
| /// <summary> | |
| /// Gets or sets the item id. | |
| /// </summary> | |
| public new string ItemID | |
| { | |
| get | |
| { | |
| return this.GetViewStateString("ItemID"); | |
| } | |
| set | |
| { | |
| Assert.ArgumentNotNull(value, "value"); | |
| this.SetViewStateString("ItemID", value); | |
| base.ItemID = value; | |
| } | |
| } | |
| /// <summary> | |
| /// Gets or sets the item version. | |
| /// </summary> | |
| public string ItemVersion | |
| { | |
| get | |
| { | |
| return this.GetViewStateString("ItemVersion"); | |
| } | |
| set | |
| { | |
| Assert.ArgumentNotNull(value, "value"); | |
| this.SetViewStateString("ItemVersion", value); | |
| } | |
| } | |
| /// <summary> | |
| /// Gets the current item. | |
| /// </summary> | |
| [Sitecore.NotNullAttribute] | |
| protected Item CurrentItem | |
| { | |
| get | |
| { | |
| if (this.currentItem != null) | |
| { | |
| return this.currentItem; | |
| } | |
| this.currentItem = Sitecore.Context.ContentDatabase.GetItem( | |
| this.ItemID, | |
| Language.Parse(this.ItemLanguage), | |
| Version.Parse(this.ItemVersion)); | |
| Assert.IsNotNull(this.currentItem, "item"); | |
| return this.currentItem; | |
| } | |
| } | |
| /// <summary> | |
| /// The on load. | |
| /// </summary> | |
| /// <param name="args"> | |
| /// The args. | |
| /// </param> | |
| protected override void OnLoad(EventArgs args) | |
| { | |
| if (!string.IsNullOrEmpty(this.Source)) | |
| { | |
| var source = this.Source; | |
| if (source.StartsWith("./")) | |
| { | |
| var rootPath = GetSite(this.CurrentItem).RootPath | |
| + source.Replace(".", string.Empty); | |
| var root = Sitecore.Context.ContentDatabase.GetItem(rootPath); | |
| if (root != null) | |
| { | |
| source = root.Paths.FullPath; | |
| } | |
| } | |
| if (source.StartsWith("query:")) | |
| { | |
| var root = this.CurrentItem.Axes.SelectSingleItem(source.Substring("query:".Length)); | |
| if (root != null) | |
| { | |
| source = root.Paths.FullPath; | |
| } | |
| } | |
| this.Source = source; | |
| } | |
| base.OnLoad(args); | |
| } | |
| /// <summary> | |
| /// The get site. | |
| /// </summary> | |
| /// <param name="item"> | |
| /// The item. | |
| /// </param> | |
| /// <returns> | |
| /// The <see cref="SiteInfo"/>. | |
| /// </returns> | |
| private static SiteInfo GetSite(Item item) | |
| { | |
| var siteInfoList = Sitecore.Configuration.Factory.GetSiteInfoList(); | |
| foreach (var siteInfo in siteInfoList) | |
| { | |
| if (item.Paths.FullPath.ToLower().Contains("/sitecore/content/" + siteInfo.Name.ToLower()) || item.Paths.FullPath.ToLower().Contains("/sitecore/templates/user defined/" + siteInfo.Name.ToLower())) | |
| { | |
| return siteInfo; | |
| } | |
| } | |
| return siteInfoList.First(n => n.Name.ToLower().Contains("website")); | |
| } | |
| } | |
| } |
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
| <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
| <sitecore> | |
| <pipelines> | |
| <getRenderingDatasource> | |
| <processor type="Site.Website.Processors.GetDatasourceLocationWithQuery, Site.Website" patch:before="processor[@type='Sitecore.Pipelines.GetRenderingDatasource.SetFallbackDatasourceLocations, Sitecore.Kernel']" /> | |
| </getRenderingDatasource> | |
| </pipelines> | |
| </sitecore> | |
| </configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment