Sitecore动态占位符允许呈现项

4
我正在按照文章中的描述,在Sitecore 7中实现动态占位符。 它的工作方式是正确的,这样我就可以将相同的渲染添加到布局中,而渲染将放置在适当的动态占位符中。然而,当我点击添加渲染到动态占位符时,占位符设置并没有被使用。
我期望的是会提示我可以放置在动态占位符上的允许渲染。相反,呈现了渲染/布局树以手动选择渲染 - 使内容编辑人员能够向占位符添加不允许的渲染。
我已经调试了代码,并找到了动态占位符的正确占位符设置项,也已检索到允许的渲染列表,但尽管在args中进行了设置,该列表并未呈现给用户。请参见下面的代码。
public class GetDynamicKeyAllowedRenderings : GetAllowedRenderings
{
    //string that ends in a GUID
    public const string DynamicKeyRegex = @"(.+){[\d\w]{8}\-([\d\w]{4}\-){3}[\d\w]{12}}";

    public new void Process(GetPlaceholderRenderingsArgs args)
    {
        Assert.IsNotNull(args, "args");

        // get the placeholder key
        string placeholderKey = args.PlaceholderKey;
        var regex = new Regex(DynamicKeyRegex);
        Match match = regex.Match(placeholderKey);

        // if the placeholder key text followed by a Guid
        if (match.Success && match.Groups.Count > 0)
        {
            // Is a dynamic placeholder
            placeholderKey = match.Groups[1].Value;
        }
        else
        {
            return;
        }

        Item placeholderItem = null;
        if (ID.IsNullOrEmpty(args.DeviceId))
        {
            placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
                                                             args.LayoutDefinition);
        }
        else
        {
            using (new DeviceSwitcher(args.DeviceId, args.ContentDatabase))
            {
                placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
                                                                 args.LayoutDefinition);
            }
        }

        // Retrieve the allowed renderings for the Placeholder
        List<Item> collection = null;
        if (placeholderItem != null)
        {
            bool allowedControlsSpecified;
            args.HasPlaceholderSettings = true;
            collection = this.GetRenderings(placeholderItem, out allowedControlsSpecified);
            if (allowedControlsSpecified)
            {
                args.CustomData["allowedControlsSpecified"] = true;
            }
        }
        if (collection != null)
        {
            if (args.PlaceholderRenderings == null)
            {
                args.PlaceholderRenderings = new List<Item>();
            }
            args.PlaceholderRenderings.AddRange(collection);
        }
    }
}

由于这段代码是为Sitecore 6.5 / 6.6开发的,我想知道升级到Sitecore 7.0是否会影响代码后半部分。
1个回答

6

我通过反编译Sitecore 7内核并查看默认的GetAllowedRenderings类找到了问题的源头。如果找到允许的渲染选项,则需要将ShowTree选项设置为false。请参见下文。

public class GetDynamicKeyAllowedRenderings : GetAllowedRenderings
    {
        //string that ends in a GUID
        public const string DynamicKeyRegex = @"(.+){[\d\w]{8}\-([\d\w]{4}\-){3}[\d\w]{12}}";

        public new void Process(GetPlaceholderRenderingsArgs args)
        {
            Assert.IsNotNull(args, "args");

            // get the placeholder key
            string placeholderKey = args.PlaceholderKey;
            var regex = new Regex(DynamicKeyRegex);
            Match match = regex.Match(placeholderKey);

            // if the placeholder key text followed by a Guid
            if (match.Success && match.Groups.Count > 0)
            {
                // Is a dynamic placeholder
                placeholderKey = match.Groups[1].Value;
            }
            else
            {
                return;
            }

            // Same as Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings from here but with fake placeholderKey
            // i.e. the placeholder without the Guid
            Item placeholderItem = null;
            if (ID.IsNullOrEmpty(args.DeviceId))
            {
                placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
                                                                 args.LayoutDefinition);
            }
            else
            {
                using (new DeviceSwitcher(args.DeviceId, args.ContentDatabase))
                {
                    placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
                                                                     args.LayoutDefinition);
                }
            }

            List<Item> collection = null;
            if (placeholderItem != null)
            {
                bool allowedControlsSpecified;
                args.HasPlaceholderSettings = true;
                collection = this.GetRenderings(placeholderItem, out allowedControlsSpecified);
                if (allowedControlsSpecified)
                {
                    // Hide the Layout/Rendering tree to show the Allowed Renderings
                    args.Options.ShowTree = false;
                }
            }
            if (collection != null)
            {
                if (args.PlaceholderRenderings == null)
                {
                    args.PlaceholderRenderings = new List<Item>();
                }
                args.PlaceholderRenderings.AddRange(collection);
            }
        }
    }

这似乎是Sitecore 7带来的一项变化。

这是一个很好的问题。理论上是可以实现的,但可能需要大量的工作。基本上,在我设置args.Options.ShowTree = false;的地方,你需要调用允许的渲染。然后,反编译Sitecore代码,找到“选择渲染”按钮点击事件的处理逻辑,并将其复制到允许渲染调用之后。 - Jonathan Robbins
这正是我现在正在寻找的...这意味着我将不得不扩展Sitecore应用渲染的实现。您知道在哪里可以找到那段代码吗? - Prathamesh dhanawade
我快速地搜索了一下,发现管道getPlaceholderRenderings获取了允许的渲染,并且Select a Rendering似乎是基于管道getRenderingDatasource的,因此请查看管道处理器的代码,你应该能够使其工作。 - Jonathan Robbins
我尝试了一下这段代码,但是找不到通用解决方案,例如,如果我想在其他渲染中使用相同的东西,是否有其他类似于为项目模板设置标准值的方法? - Prathamesh dhanawade
我已经发布了一个 https://dev59.com/6I_ea4cB1Zd3GeqPUutV - Prathamesh dhanawade
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接