Customizing rich text fields in Sitecore on accept action

Objective:

Post process rich text field values entered by content authors when the RTE ‘Accept’ action is triggered (Sitecore 6.5).

Introduction:

Sitecore provides a Rich Text Field which basically lets the content author use either the inbuilt rich text editor or pure HTML to insert rich text content in a given item.

In some scenarios we might want to have some level of control over the entered text programmatically. The following snippet gives you access to the text entered by the content author, when they click on accept from the rich text editor and lets you parse it and then replace the entered text with your processed text.

Please note, the pipeline in use here is not used if the content author uses the ‘Edit Html’ action from the RTF straight up!

2014-04-26_134449

But do note that you can edit Html even from the rich text editor itself!

2014-04-26_134619

Implementation:

Really the only important thing here is to use the right pipeline, provide accurate assembly / class names and use the right method signature in the class!

Here is the configuration update to add to the saveRichTextContent pipeline.


<pipelines>
	<saveRichTextContent>
        	<processor type="Sitecore.Shell.Controls.RichTextEditor.Pipelines.SaveRichTextContent.EmbedInParagraph, Sitecore.Client" />
        	<processor type="Mysite.Global.Classes.SaveRelativeLinks, Mysite" />
	</saveRichTextContent>
</pipelines>

Since the content of a rich text editor is primarily Html, you could use the HtmlAgilityPack to access html nodes with ease. In the below example, we are looping to every anchor tag in the rich text field and we could possibly transform it or even remove the anchor tag if the href url didn’t pass through certain filters!


    public class SaveRelativeLinks
    {
        public void Process(SaveRichTextContentArgs args)
        {
            // Load the HTML into the HtmlAgilityPack
            var doc = new HtmlDocument { OptionWriteEmptyNodes = true };
            doc.LoadHtml(args.Content);

            // Search for all links
            var aTags = doc.DocumentNode.SelectNodes("//a");
            if (aTags == null || aTags.Count == 0)
                return;

            foreach (var node in aTags)
            {
                if (node.Attributes["href"] != null
                    && !string.IsNullOrEmpty(node.Attributes["href"].Value))
                {
                    // perform any logic you'd want on the node here!
                    // ...
                    // ...
                }
            }
            // Replace the Rich Text content with the modified content
            args.Content = doc.DocumentNode.OuterHtml;
        }
    }

Advertisements

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