Sea*_*sby 6 sitecore sitecore6
我有一个渲染参数模板应用于子布局.它上面有一个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 Update 5)
不幸的是,Adam Najmanowicz的答案中提到的管道适用于其他类型,如Droplink和Multilist,但管道不适用于Droptree字段.
在深入研究之后,我发现Droptree字段的Source使用了错误的上下文项,正如Adam所提到的,但代码来自Droptree字段本身: -
Sitecore.Shell.Applications.ContentEditor.Tree, Sitecore.Kernel
利用Adam的答案中的查询字符串代码,我们可以创建一个"固定的"Droptree自定义字段,它几乎与常规Droptree相同,但会使用正确的上下文项.代码将继承正常的Tree控件,并且只更改Source属性的设置方式.
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
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码与基本树字段上的Source属性几乎相同,只是如果我们检测到我们在渲染参数对话框中,我们会从URL中找出正确的上下文项.
要创建自定义字段,只需编辑此处所述的Web.Config文件即可.如所描述的自定义字段,然后添加到核心数据库这里.
这意味着参数现在可以查询其源,允许我们将可用项限制为内容编辑器.(适用于多站点解决方案).
这里的关键是将Field Editor的上下文设置为相对于您正在编辑的项而不是Rendering参数(我认为它默认具有).所以你可以有处理器:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
迷上了:
<sitecore>
<pipelines>
<getLookupSourceItems>
<processor patch:before="*[@type='Sitecore.Pipelines.GetLookupSourceItems.ProcessQuerySource, Sitecore.Kernel']"
type="Cognifide.SiteCore.Logic.Processors.ResolveRelativeQuerySource, Cognifide.SiteCore" />
</getLookupSourceItems>
</pipelines>
</sitecore>
Run Code Online (Sandbox Code Playgroud)
与Przemek博客中的ResolveQueryableDatasources一起解决您的问题.
| 归档时间: |
|
| 查看次数: |
2789 次 |
| 最近记录: |