Insert dynamic text into Sitecore Rich Text field

richtextbox, sitecore

Solution

You can add your own processor to the `renderField` pipeline, check whether current field is RichText field and replace a token (e.g. __YEAR__) with current year:

<renderField>
  <!--... other processors -->
  <processor type="My.Assembly.Namespace.ReplaceTokenProcessor, My.Assembly" />
  <!--... other processors -->
</renderField>

and the code of the processor:

namespace My.Assembly.Namespace
{
  public class ReplaceTokenProcessor
  {
    public virtual void Process(RenderFieldArgs args)
    {
      if (args.FieldTypeKey != "rich text")
        return;

      args.Result.FirstPart = (args.Result.FirstPart == null) ? null : args.Result.FirstPart.Replace("__YEAR__", DateTime.Now.Year.ToString());
      args.Result.LastPart = (args.Result.LastPart == null) ? null : args.Result.LastPart.Replace("__YEAR__", DateTime.Now.Year.ToString());
    }
  }
}

Problem

I've been spending a bit of time with Sitecore recently, and I noticed the site I am working on has a copyright date in a `Rich Text` field. Unfortunately, it is 2012. Now, the easy way to fix this problem is to simply go in and change the `Rich Text` field which has the copyright information, but I don't want to have to worry about changing it in 2014. Is there a way to insert dynamic text into the `Text` control? Even if I could have a sigil which I could manually replace in C# that would be preferable to either switching the `Text` for a `Literal` or force manual updates every year.

Original source