Wrapping Rich Text Value in paragraph tag in Sitecore

Often we find the situation where the front end requires all text coming in from rich text fields in sitecore to be wrapped in say a <p> tag, for styling purposes.
In most cases, it is a better option to achieve this through code, than leave it up to the content authors.

You could go with one of the following 2 approaches:

You could create a new pipeline event in the

  • <saveRichTextContent> pipeline – This could enable you to append the <p> tag when you hit save on the rich text editor in sitecore
  • <renderField> pipeline – This could on the fly wrap your text into <p></p> tag while rendering the page, if the <p> tag was not there in the original rtf text.

If you go for method 1: <saveRichTextContent>
You could add to the pipeline in web.config:

<processor type="Sitecore72.Classes.WrapRichTextInParagraphOnSave, Sitecore72" />

And you could use the following corresponding code:

    namespace Sitecore72.Classes
    {
        public class WrapRichTextInParagraphOnSave
        {
            public void Process(SaveRichTextContentArgs args)
            {
                if (!(args.Content.Trim().StartsWith("<p>") && args.Content.Trim().EndsWith("</p>")))
                    args.Content = "<p>" + args.Content + "</p>";
            }
        }
    }

Please note, that this pipeline gets triggered only when you use the Show Editor buttong of a rich text field:
2014-06-14_215418

If you go for method 2: <renderField>

To append to this pipeline you would use this config:

<processor type="Sitecore72.Classes.WrapRichTextInParagraphOnRender, Sitecore72" />

And you could use the following corresponding code:

    namespace Sitecore72.Classes
    {
        public class WrapRichTextInParagraphOnRender
        {
            public void Process(RenderFieldArgs args)
            {
                if (args.FieldTypeKey == "rich text" && !(args.Result.FirstPart.Trim().StartsWith("<p>") && args.Result.FirstPart.Trim().EndsWith("</p>")))
                    args.Result.FirstPart = "<p>" + args.Result.FirstPart + "<//p>";
            }
        }
    }

For both these, ensure you add reference to Sitecore.Kernel.dll.

Advertisements

, , , , , , ,

  1. #1 by Mike Reynolds on June 16, 2014 - 7:32 am

    Both of these pipelines offer some great entry points for customization in Sitecore.

    I have also dabbled with the in a past post:

    http://sitecorejunkie.com/2013/02/08/dude-wheres-my-processor-filling-the-void-in-the-saverichtextcontent-and-loadrichtextcontent-sitecore-pipelines/

    Plus, I have had lots of fun adding processors to the pipeline in a few posts:

    http://sitecorejunkie.com/category/render-field/

    Mike

    Like

  2. #2 by Mike Reynolds on June 16, 2014 - 7:35 am

    Apologies: the pipeline names were stripped by WP due to an Html encoding issue. I was referring to the <saveRichTextContent> and <renderField> pipelines, respectively.

    Mike

    Liked by 1 person

  1. Understanding Glass: The issue with the Editable Method and Paragraph Tag – Eric Stafford

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: