gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:51:41 28 4
gpt4 key购买 nike

我正在按照文章中的描述在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com