Enabling HTML tags in Single-line Text Fields

When I first started working with Sitecore, back in version 5.3, it supported HTML tags in a Single-line text field – if you placed HTML tags such as <b> or <em> in a SLT field, the tags would go through and render as HTML on the front-end.  Though uncommon, this was a nice feature – especially if you have tech savvy Content Authors.  Sitecore has released many upgrades since the old days of 5.3 and in one of these upgrades, Sitecore updated the Sitecore.Pipelines.RenderField.GetTextFieldValue class located in Sitecore.Kernel to encode HTML tags in a SLT field.  Here’s the decompiled version of this class in the latest version of Sitecore 7.2:

using Sitecore.Diagnostics;
 using System;
 using System.Web;
 namespace Sitecore.Pipelines.RenderField
     /// <summary>
     /// The get text field value.
     /// </summary>
     public class GetTextFieldValue
         /// <summary>
         /// Gets the field value.
         /// </summary>
         /// <param name="args">
         /// The arguments.
         /// </param>
         /// <contract>
         ///   <requires name="args" condition="none" />
         /// </contract>
         public void Process(RenderFieldArgs args)
             Assert.ArgumentNotNull(args, "args");
             string fieldTypeKey = args.FieldTypeKey;
             if (fieldTypeKey != "text" && fieldTypeKey != "single-line text")
             args.Result.FirstPart = HttpUtility.HtmlEncode(args.Result.FirstPart);

Notice that last line that calls HttpUtility.HtmlEncode.  This line makes it so that if you enter something like <em></em> in your SLT field, when you put that field through the RenderField pipeline, you get this:


To allow my Content Authors to use HTML tags in SLT fields, I implemented a class called CustomGetTextFieldValue which is basically a copy of the class above but I leave out the HttpUtility.HtmlEncode line.  The final step is to patch this class into the pipeline instead of the built-in class.  For this, I created a config file named CustomGetTextFieldValue.config and placed it in my \App_Config\Include folder.  Here’s the content of the config file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
                    type="MyProject.Framework.Pipelines.RenderField.CustomGetTextFieldValue, MyProject.Framework"
                    patch:instead="processor[@type='Sitecore.Pipelines.RenderField.GetTextFieldValue, Sitecore.Kernel']" />

Leave a Reply