Customizing Sitecore rich text field configuration

People often ask me questions on how to configure the Sitecore rich text editor in various ways.

The Sitecore rich text editor is really easy to control and customize and there are already a lot of blog posts about it out there.

Most of these posts only show how to create custom editor profiles and not how to override the actual editor configuration.

I guess everyone knows how to create custom rich text editor profiles by now so this is not what I want to show. If this is what your are looking for then just ask google.

Overriding the default rich text editor configuration

The rich text field is a Telerik RadControl and can be controlled completely in code which is the way I prefer to do it.

This is done in Sitecore by overriding a class called Sitecore.Shell.Controls.RichTextEditor.EditorConfiguration. This class has an apply method which gets called each time a rich text editor is opened.

There are some methods on the EditorConfiguration which you would normally override to inject desired custom behaviour

SetupFilters()

The SetupFilters method is probably the most interesting one. This method is used to set which filters to use on the content that the editors type in.

The standard SetupFilters method can enable or disable whether or not Script is allowed in rich text fields. This is controlled using the Sitecore setting called HtmlEditor.RemoveScripts

This setting control the filter EditorFilters.RemoveScripts

It is not always sufficient to control this behaviour on all editor profiles using the setting. So it does make sense in some cases to override this method and then set the Configuration Type only on a specific Html Editor profile in core.

The below code example shows how other filters can be enabled and disabled.

For a complete list of EditorFilters see the Telerik documentation here http://www.telerik.com/help/aspnet-ajax/editor-content-filters.html.

 SetupStylesheets()

The SetupStylesheets method loads in the styles to use in the rich text editor. By default Sitecore will load the stylesheet defined in the setting called WebStylesheet from configuration. This is rarely sufficient on multi site solutions.

Below is an example on how to load in more stylesheets in the rich text editor by overriding this method.

I will not encourage you to hardcode the path as I just did in the above example but instead use Sitecore.Configuration.Settings.Get( … ) to get your own custom configuration setting.

SetupFontSizes()

The SetupFontSizes method is used to set what font sizes that are shown in the rich text editor drop down that the editors can choose from.

For more information on Real Font sizes see Telerik’s documentation here http://www.telerik.com/help/aspnet-ajax/editor-real-font-sizes.html

SetupEditor()

The SetupEditor method is used to set a various set of general configuration options.  For example reading the newline mode from settings and setting the padding on the content area in the rich text field. It is not that common that you would like to override this method but there can be some cases where the global setting does not fit all sites.

You should ALWAYS call base.SetupEditor() first in your overridden method.

The EditorConfiguration class contains other methods that can be overridden as well but you would normally not have a good reason to do this.

Configuring what EditorConfiguration type to use where

Sitecore have two levels where you can set your custom EditorConfiguration type.

You can set the general default EditorConfiguration in web.config by patching in the following Sitecore setting in an include config file.

Or you can set the EditorConfiguration type on the specific HtmlEditor profile as shown below.

editoconfiguration

 

This is done by creating a Html Editor Configuration Type item beneath a html editor profile in core and then point the Type field to your derived class.

Which way to go depends on the solution and the desired control granularity.

The ToolsFile.xml and why not to use it

You can also alter the configuration of the rich text editor by modifying the file /sitecore/shell/Controls/Rich Text Editor/ToolsFile.xml

You can see the Telerik documentation by following the link below but before you do that I will warn you about not changing this file in your Sitecore solution. This is a file that ships with Sitecore and can potentially be updated in other versions of Sitecore than the one your solution is running. The changes made in this file will also affect all rich text fields, there is no way of making it profile dependent as there is when using code.

http://www.telerik.com/help/aspnet-ajax/editor-using-toolsfile.html

That was it, I hope this post helps someone.

Please feel free to share and comment.

Anders Laub

Anders Laub Christoffersen

Anders has been working with Sitecore for over a decade and has in this time been the lead developer and architect on several large scale enterprise solutions all around the world. Anders has been nominated a Sitecore Technical MVP three years in a row for 2014, 2015 and 2016. Anders is now working as a Sr. Solutions Architect at Sitecore in Copenhagen.

2 thoughts on “Customizing Sitecore rich text field configuration

  1. hey Anders

    Is there a way i can replace the current Telerik RTE with another RTE, if so can you provide some details of how i can do that.

    Thanks
    Navjyot Singh

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website