Adding a custom button to the Rich Text Editor's toolbar in Sitecore 7

MattKloss
Senior VP, Digital Delivery
  • Twitter
  • LinkedIn

Adding a custom button to the Rich Text Editor's toolbar in Sitecore 7

Recently I had requirements from a client to add a few custom buttons to the Rich Text Editor s toolbar. While I found this blog particularly helpful, it didn t quite work for me in Sitecore 7. Furthermore, I have a few best-practice tips. So here it is a step by step guide to adding a button to the Rich Text Editor. Login to Sitecore as an admin and switch over to the Core database. Navigate to /sitecore/system/Settings/Html Editor Profiles In here you will see a folder for each of the toolbars, its best not to mess with any of the default Sitecore toolbars, so make a copy of Rich Text Full In my example, I named it Rich Text AR Full Drill down into the newly created directory and you should see Toolbar 1. Right click on Toolbar1 and insert __Html Editor Button Give it a name and a value for the Click Event. Lastly change the icon by click on Configure in the toolbar and selecting the Icon button. In your web.config, be sure and update the HtmlEditor.DefaultProfile value to point to the newly created toolbar, in my case Rich Text AR Full Open up windows explorer go to your website directory. Browse to \sitecore\shell\Controls\Rich Text Editor. In there you ll see InsertImage Copy that sucker within the same directory and rename it to whatever you named your button, in my case QuoteBig Rename both the xml and js file to the button s name Open the js file. In here we are looking for one function scClose In my case, I am prompting the user to insert some text. So I want to update the return type with text in place of image. My new scClose function looks like this:

function scClose(text) {
var returnValue = {
text:text
};
getRadWindow().close(returnValue);
}

Now - open the xml file. Make the following updates:

  • Update the RichText tag to include your button s name.
  • Change the Form Dialog to point to the icon you selected and update the helper text
  • In the script tag, be sure it is pointing to the js file we updated in the previous step.
  • For the CodeBehind, we ll have to come back and update this node

This is how mine looks

 
     
         
             
             
             
                 
             
         
     

Next we need to create and compile the class. I created an XmlControls directory in the root of my websitefolder and added the new class there. Here is what my class looks like. Take note of the namespace as you ll need to update your xml file to point to this class. Also, within the OnOK function, you ll notice I added some html around the content. This is where you can do any kind of alteration to the user s input that you d like.

namespace AR.Website.XmlControls
{
    public class QuoteBig : DialogForm
    {
        protected Sitecore.Web.UI.HtmlControls.Memo memCode;

        protected override void OnLoad(EventArgs e)
        {
            Assert.ArgumentNotNull(e, "e");
            base.OnLoad(e);
            if (!Context.ClientPage.IsEvent)
            {
                this.Mode = WebUtil.GetQueryString("mo");
                string text = WebUtil.GetQueryString("selectedText");
                memCode.Value = text;
            }
        }

        protected override void OnOK(object sender, EventArgs args)
        {
            Assert.ArgumentNotNull(sender, "sender");
            Assert.ArgumentNotNull(args, "args");

            string content = "
" + memCode.Value + "
"; if (this.Mode == "webedit") { SheerResponse.SetDialogValue(StringUtil.EscapeJavascriptString(content)); base.OnOK(sender, args); } else SheerResponse.Eval("scClose(" + StringUtil.EscapeJavascriptString(content) + ")"); } protected override void OnCancel(object sender, EventArgs args) { Assert.ArgumentNotNull(sender, "sender"); Assert.ArgumentNotNull(args, "args"); if (this.Mode == "webedit") base.OnCancel(sender, args); else SheerResponse.Eval("scCancel()"); } protected string Mode { get { string str = StringUtil.GetString(base.ServerProperties["Mode"]); if (!string.IsNullOrEmpty(str)) return str; return "shell"; } set { Assert.ArgumentNotNull(value, "value"); base.ServerProperties["Mode"] = value; } } } }

Lastly, we ll need to go update the RichText Commands.js located within website\sitecore\shell\Controls\Rich Text Editor.

RadEditorCommandList["QuoteBig"] = function (commandName, editor, args) {
var html = editor.getSelectionHtml();
scEditor = editor;
editor.showExternalDialog(
"/sitecore/shell/default.aspx?xmlcontrol=RichText.QuoteBig&la=" + scLanguage + "&selectedText=" + escape(html),
null,
500, //width
200, //height
scInsertQuote, //callback
null,
"Insert Quote",
true, //modal
Telerik.Web.UI.WindowBehaviors.Close, // behaviors
false, //showStatusBar
false //showTitleBar
);
};

function scInsertQuote(sender, returnValue) {
if (!returnValue) {
return;
}
scEditor.pasteHtml(returnValue.text, "DocumentManager");
}

Be sure and clear your sitecore cache and test in a private browsing window to ensure you're getting all of your changes. That should be all it takes!

Related Blogs

Latest Blogs