【问题标题】:sitecore wrapping for tags pipeline in fields are not working字段中标签管道的 sitecore 包装不起作用
【发布时间】:2015-06-18 08:37:26
【问题描述】:

我正在使用 sitecore 7.2 并且我已经创建了用于封闭单行文本标签的管道,如下所示。

public class SingleLineFieldEnclosingTags
    {
        public void Process(RenderFieldArgs args)
        {
            if (args.FieldTypeKey != "single-line text" && args.FieldTypeKey != "multi-line text")
                return;

            args.Result.FirstPart = Helper.WrapInTags(args);            
        }
    }

public static string WrapInTags(RenderFieldArgs args)
        {
            string keyInParam = args.Parameters.Where(x => x.Key.Contains("enclosingTag")).FirstOrDefault().Key;
            string wrappedText = args.Result.FirstPart;

            if (IsPageEditorMode)
            {
                return wrappedText;
            }

            if (keyInParam != null && keyInParam.Trim().Equals("enclosingTag"))
            {
                if (args.Parameters.ContainsKey(keyInParam))
                {
                    string[] paramTags = args.Parameters[keyInParam].Split('|').Reverse().ToArray();

                    foreach (string tag in paramTags)
                    {
                        wrappedText = string.Concat("<", tag.Trim(), ">", wrappedText, "</", tag.Trim().Split(' ')[0], ">");
                    }

                    if (wrappedText.Contains("enclosingTag"))
                    {                            
                        // remove enclosing tag attribute from tags
                        wrappedText = Regex.Replace(wrappedText, @"enclosingTag\s*=\""\s*?.*\""", string.Empty, RegexOptions.IgnoreCase);
                    }
                    return wrappedText;
                }
            }
            return wrappedText;
        }

它工作正常,但是当我们编辑或保存任何字段然后将其另存为时出现问题

<p class="intro">Do you need something specific?</p>

也包括封闭标签。现在在发布模式下它显示两次,三次,如下所示。

<p class="intro"></p>
<p class="intro"></p>
<p class="intro"></p>
<p class="intro">Do you need something specific?</p>
<p></p>
<p></p>
<p></p>

【问题讨论】:

    标签: asp.net sitecore pipeline pipelining page-editor


    【解决方案1】:

    我假设您已将此管道添加到 Item:Saved。正如您所发现的,发布实际上还会在 Web 数据库中创建/保存项目,该服务器上会触发相同的事件,因此您会看到重复的标签。添加检查以确保您在 process 方法中的主数据库中运行:

    public void Process(RenderFieldArgs args)
    {
        if (!args.Item.Database.Name.Equals("master", StringComparison.InvariantCultureIgnoreCase))
            return;
    
        if (args.FieldTypeKey != "single-line text" && args.FieldTypeKey != "multi-line text")
            return;
    
        args.Result.FirstPart = Helper.WrapInTags(args);            
    }
    

    但是,您可能需要检查您的文本是否已经包含在您所包含的标签中(以及) - 使用 HTMLAgilityPackCsQuery 而不是 messing with regular expressions。在single-line text 字段等简单场景中,您可能只需使用StartsWith() 检查即可。

    您应该考虑将您的代码移至renderField 管道(更多信息在此blog postthis one),或使用FieldRendererEnclosingTag 属性。这样标签是在渲染时添加的,而不是出现在内容编辑器中。在我之前发布的this SO answer 中有一些代码,您也可以使用它来传递 css 类。

    【讨论】:

    • 顺便说一句,FieldTypeKey 检查不应该是 OR (||) 检查吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多