GlassMapper渲染自定义链接字段

时间:2016-11-23 11:25:30

标签: razor sitecore glass-mapper sitecore8.1

我们使用的是sitecore 8.1 update 3和Glass Mapper 4.2.1.188

对于普通的Link字段,它在经验丰富的编辑器和普通模式下工作正常。

我们在核心数据库中复制了General Link字段并删除了“Javascript”菜单项。这是我们为自定义链接字段所做的唯一更改。

这使得字段在经验编辑器模式中消失。它在正常模式下很好。

@RenderLink(x => x.CallToActionButton, new { @class = "c-btn c-btn--strong c-btn--large" }, isEditable: true)

编辑1:

当我使用Sitecore Field渲染器时它很好。

 @Html.Sitecore().Field(FieldIds.HomeCallToActionButton, new { @class = "c-btn c-btn--strong c-btn--large" })

任何建议都将受到赞赏。

1 个答案:

答案 0 :(得分:2)

您遇到问题的原因是Sitecore会在生成体验编辑器中显示的代码时检查字段类型键。

Sitecore.Pipelines.RenderField.GetLinkFieldValue班级检查字段类型键是link还是general link,并且根据您所写的内容,您复制了原始General Link字段,因此您的字段名称是Custom Link或类似的东西。这意味着您的案例中的字段类型键为custom link(字段类型名称小写)。 SkipProcessor方法将custom link与字段类型键进行比较,因为它不同,处理器会忽略您的字段。

您不能简单地将字段重命名为General Link并将其放在Field Types/Custom文件夹下,因为Sitecore不会保留字段类型的ID(它会存储字段类型键)。

你可以做的是覆盖Sitecore.Pipelines.RenderField.GetLinkFieldValue类及其中的一个方法:

using Sitecore.Pipelines.RenderField;

namespace My.Assembly.Namespace
{
    public class GetLinkFieldValue : Sitecore.Pipelines.RenderField.GetLinkFieldValue
    {
        /// <summary>
        /// Checks if the field should not be handled by the processor.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns>true if the field should not be handled by the processor; otherwise false</returns>
        protected override bool SkipProcessor(RenderFieldArgs args)
        {
            if (args == null)
                return true;
            string fieldTypeKey = args.FieldTypeKey;
            if (fieldTypeKey == "custom link")
                return false;
            return base.SkipProcessor(args);
        }
    }
}

并注册而不是原始类:

<sitecore>
  <pipelines>
    <renderField>
        <processor type="Sitecore.Pipelines.RenderField.GetLinkFieldValue, Sitecore.Kernel">
           <patch:attribute name="type">My.Assembly.Namespace.GetLinkFieldValue, My.Assembly</patch:attribute>
        </processor>
    </renderField>
  </pipelines>
</sitecore>
相关问题