在“渲染参数模板”字段上设置可查询源

时间:2013-08-02 05:38:21

标签: 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)

2 个答案:

答案 0 :(得分:7)

不幸的是,Adam Najmanowicz's answer中提到的管道适用于其他一些类型,例如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
        }
    }

上面的代码与基本 Tree 字段上的Source属性几乎相同,只是如果我们检测到我们在,那么我们会从URL中找出正确的上下文项目。渲染参数对话框。

要创建自定义字段,您只需按照here所述编辑Web.Config文件。然后按照here所述将自定义字段添加到核心数据库。

这意味着参数现在可以查询其源,允许我们将可用项限制为内容编辑器。 (适用于多站点解决方案)。

答案 1 :(得分:5)

这里的关键是将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;
        }
    }
}

迷上了:

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

与Przemek博客中的ResolveQueryableDatasources一起解决您的问题。