c# - Sitecore 动态占位符允许渲染

标签 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 时是否会带来影响代码后半部分的更改

最佳答案

我通过反编译 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 带来的变化。

关于c# - Sitecore 动态占位符允许渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25646473/

相关文章:

sitecore - 如何在 Sitecore 中调试代码

c# - SiteCore .Net 成员(member)示例

c# - 使用超过 4 个 BackgroundWorker 时线程不会立即运行

c# - 域驱动设计中每个实体的创建者、编辑者、创建日期、修改日期属性

c# - 这里的编译时类型安全是什么意思?

.net - 将DynamicMethod保存到磁盘

sitecore - 使用 TextTransform.exe 在 sitecore 中生成 TDS 代码

c# - 在 C++ 中,我可以有一个函数/方法通过引用传递一个参数,而一个重载通过值传递它吗?

c# - 是否有 LINQ 等效方法?

c# - c# 中的 GetEnumerator() 是否返回副本或迭代原始源?