Sitecore动态占位符允许渲染

时间:2014-09-03 14:06:16

标签: c# .net sitecore sitecore7

我正如文章

中所述在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 个答案:

答案 0 :(得分: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带来的变化。