我有一个Sitecore 8 MVC解决方案,我必须扩展数据源的行为。它与其他人之前使用可查询数据源所做的非常类似(例如http://www.cognifide.com/blogs/sitecore/reduce-multisite-chaos-with-sitecore-queries/等),但我已经挂钩到<mvc.getXmlBasedLayoutDefinition>
管道。它工作正常,我的自定义数据源在项目的布局字段或标准值中输入时会被解析。
但是,当自定义数据源被指定为渲染项目的默认数据源时,事情会变得有点棘手。我可以通过相同的管道解决它,但该解决方案看起来不是很好。这意味着我必须加载每个没有布局中指定的数据源的渲染,并进行处理并从那里解决它。必须有更自然的方式来做到这一点。
有谁知道在哪里为默认数据源放置这样的实现逻辑? (<resolveRenderingDatasource>
管道看起来很有希望,但在这种情况下没有执行)
答案 0 :(得分:2)
根据我的理解,您可能希望扩展XmlBasedRenderingParser
课程。以下是应该采取措施的步骤:
App_Config\include\Sitecore.Mvc.Custom.config
:<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<initialize>
<processor
patch:after="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']"
type="My.Assembly.Namespace.RegisterCustomXmlBasedRenderingParser, My.Assembly"/>
</initialize>
</pipelines>
</sitecore>
</configuration>
CustomXmlBasedRenderingParser
类:using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Mvc.Extensions;
using Sitecore.Mvc.Presentation;
namespace My.Assembly.Namespace
{
public class CustomXmlBasedRenderingParser : XmlBasedRenderingParser
{
protected override void AddRenderingItemProperties(Rendering rendering)
{
RenderingItem renderingItem = rendering.RenderingItem;
if (renderingItem != null && !rendering.DataSource.ContainsText())
{
rendering.DataSource = ResolveRenderingItemDataSource(renderingItem);
}
base.AddRenderingItemProperties(rendering);
}
private static string ResolveRenderingItemDataSource(RenderingItem renderingItem)
{
string dataSource = string.Empty;
if (renderingItem.DataSource != null && renderingItem.DataSource.StartsWith("query:"))
{
string query = renderingItem.DataSource.Substring("query:".Length);
Item contextItem = Context.Item;
Item queryItem = contextItem.Axes.SelectSingleItem(query);
if (queryItem != null)
{
dataSource = queryItem.Paths.FullPath;
}
}
return dataSource;
}
}
}
RegisterCustomXmlBasedRenderingParser
类:using Sitecore.Mvc.Configuration;
using Sitecore.Mvc.Presentation;
using Sitecore.Pipelines;
namespace My.Assembly.Namespace
{
public class RegisterCustomXmlBasedRenderingParser
{
public virtual void Process(PipelineArgs args)
{
MvcSettings.RegisterObject<XmlBasedRenderingParser>(() => new CustomXmlBasedRenderingParser());
}
}
}
此外,如果您希望在渲染和演示详细信息中定义的DataSource
执行代码,您应该能够使用以下代码:
using System.Xml.Linq;
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Mvc.Presentation;
namespace My.Assembly.Namespace
{
public class CustomXmlBasedRenderingParser : XmlBasedRenderingParser
{
public override Rendering Parse(XElement node, bool parseChildNodes)
{
Rendering rendering = base.Parse(node, parseChildNodes);
ResolveRenderingItemDataSource(rendering);
return rendering;
}
private static void ResolveRenderingItemDataSource(Rendering rendering)
{
if (rendering.DataSource != null && rendering.DataSource.StartsWith("query:"))
{
string query = rendering.DataSource.Substring("query:".Length);
Item contextItem = Context.Item;
Item queryItem = contextItem.Axes.SelectSingleItem(query);
if (queryItem != null)
{
rendering.DataSource = queryItem.Paths.FullPath;
}
}
}
}
}
请记住,此代码未经过正确测试,可能无法在您的环境中开箱即用。无论如何,我希望它至少会给你一个很好的指示从哪里开始。