Set the requested source in the "Rendering Parameter Template" field

I have a render parameter template applied to a sublayer. It has one Droptree field on it, and I want to set the source of this field to the Sitecore request so that I can limit the options available for this field.

The source may be:

query:./*

or

query:./ancestor-or-self::*[@@templatename='MyTemplate']/

The request simply needs to capture the elements relative to the content element that we are in. This usually works with Droptree fields in the content editor.

However, I found that the request does not work here, because we are in the rendering options, so it does not use the content element as context. The request fails, and I just get the full Sitecore tree.

I found that this can be fixed for the Datasource field with "Queryable Datasource Locations" at this link: - http://www.cognifide.com/blogs/sitecore/reduce-multisite-chaos-with-sitecore-queries/

However, I don’t know where to start this work for other fields of rendering parameters.

Any ideas? (I am using Sitecore 6.6 Update 5)

+6
source share
2 answers

Unfortunately, the pipeline mentioned in the Adam Najmanowicz answer works for some other types, such as Droplink and Multilist, but the pipeline does not start for Droptree fields.

Having examined this more deeply, I found that the source of the Droptree IS field is using the wrong context element, as Adam mentioned, but the code comes from the Droptree field itself: -

Sitecore.Shell.Applications.ContentEditor.Tree, Sitecore.Kernel

Using the query string code from Adam’s answer, we can create a β€œfixed” custom droptree field that is almost the same as a regular droptree, but will instead use the correct context element. The code inherits from the regular Tree control and only changes the way the Source property is set.

 public class QueryableTree : Sitecore.Shell.Applications.ContentEditor.Tree { // override the Source property from the base class public new string Source { get { return StringUtil.GetString(new string[] { base.Source // slightly altered from the original }); } set { Assert.ArgumentNotNull(value, "value"); if (!value.StartsWith("query:", StringComparison.InvariantCulture)) { base.Source = value; // slightly altered from the original return; } Item item = Client.ContentDatabase.GetItem(this.ItemID); // Added code that figures out if we're looking at rendering parameters, // and if so, figures out what the context item actually is. string url = WebUtil.GetQueryString(); if (!string.IsNullOrWhiteSpace(url) && url.Contains("hdl")) { FieldEditorParameters parameters = FieldEditorOptions.Parse(new UrlString(url)).Parameters; var currentItemId = parameters["contentitem"]; if (!string.IsNullOrEmpty(currentItemId)) { Sitecore.Data.ItemUri contentItemUri = new Sitecore.Data.ItemUri(currentItemId); item = Sitecore.Data.Database.GetItem(contentItemUri); } } if (item == null) { return; } Item item2 = item.Axes.SelectSingleItem(value.Substring("query:".Length)); if (item2 == null) { return; } base.Source = item2.ID.ToString(); // slightly altered from the original } } 

The above code is almost the same as the Source property in the Tree field of the tree, except that we calculate the correct context element from the URL if we find that we are in the rendering options dialog box.

To create a custom field, you just need to edit the Web.Config file as described here . Then add the custom field to the base database as described here .

This means that now parameters can have queries for their source, which allows us to restrict the available elements to the content editor. (Useful for multisite solutions).

+7
source

The key point here would be to set the context of the field editor relative to the element you are editing, instead of the rendering options (in my opinion, it is the default). So you could have a processor:

 public class ResolveRelativeQuerySource { public void Process(GetLookupSourceItemsArgs args) { Assert.IsNotNull(args, "args"); if (!args.Source.StartsWith("query:")) return; Item contextItem = null; string url = WebUtil.GetQueryString(); if (!string.IsNullOrWhiteSpace(url) && url.Contains("hdl")) { FieldEditorParameters parameters = FieldEditorOptions.Parse(new UrlString(url)).Parameters; var currentItemId = parameters["contentitem"]; if (!string.IsNullOrEmpty(currentItemId)) { Sitecore.Data.ItemUri contentItemUri = new Sitecore.Data.ItemUri(currentItemId); contextItem = Sitecore.Data.Database.GetItem(contentItemUri); } } else { contextItem = args.Item; } } } 

connected as:

 <sitecore> <pipelines> <getLookupSourceItems> <processor patch:before="*[@type='Sitecore.Pipelines.GetLookupSourceItems.ProcessQuerySource, Sitecore.Kernel']" type="Cognifide.SiteCore.Logic.Processors.ResolveRelativeQuerySource, Cognifide.SiteCore" /> </getLookupSourceItems> </pipelines> </sitecore> 

Together with the ResolveQueryableDatasources from the Przemek blog, this should solve your problem.

+5
source

Source: https://habr.com/ru/post/950871/


All Articles