在渲染参数模板字段上设置可查询源 [英] Set queryable source on Rendering Parameter Template field
问题描述
我将渲染参数模板应用到子布局。它只有一个Droptree字段,并且我要将该字段的Source设置为Sitecore查询,以便可以限制该字段的可用选项。
来源可以是:
query:./*
或
query:./ancestor-or-self::*[@@templatename='MyTemplate']/
查询只需要获取与我们所在的内容项相关的项。这通常适用于内容编辑器中的Droptree字段。
但是,我发现查询在这里不起作用,因为我们在呈现参数中,所以它没有使用内容项作为它的上下文。 查询失败,我只获得完整的Sitecore树。我发现可以通过以下链接中的"可查询数据源位置"修复数据源字段的这一问题:- http://www.cognifide.com/blogs/sitecore/reduce-multisite-chaos-with-sitecore-queries/
但是,我不知道从哪里开始对其他呈现参数字段执行此操作。
有什么想法吗?(我正在使用Sitecore 6.6更新5)
推荐答案
不幸的是,Adam Najmanowicz's answer中提到的管道可用于其他一些类型,如Droplink和MultiList,但管道不能为Droptree字段运行。
深入研究后,我发现Droptree字段的Source使用了错误的上下文项,就像Adam提到的那样,但是代码来自Droptree字段本身:-
Sitecore.Shell.Applications.ContentEditor.Tree, Sitecore.Kernel
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
}
}
上述代码与基本树字段上的Source属性基本相同,不同之处在于,如果我们检测到我们在"呈现参数"对话框中,则会从URL中找出正确的上下文项。
要创建自定义域,您只需按照here的说明编辑Web.Config文件。然后将自定义字段添加到核心数据库,如here所述。
这意味着参数现在可以查询它们的源,允许我们将可用项限制到内容编辑器。(适用于多站点解决方案)。
这篇关于在渲染参数模板字段上设置可查询源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!