24
Jul
35
Customising the WYSYWIG editor in v2.3.2 (TinyMCE)
The default install of SilverStripe contains an array of buttons for the TinyMCE HTML editor. However usually your client will need some extra functionality or you may want to keep things simple for them by removing some of the buttons. As of SilverStripe 2.3.2 this is very easy and can be achieved by adding some code to the _config.php file.
Note. if you are using a version <2.3.2 you must edit the file cms/javascript/tinymce.template.js.
To find out all the wonderful buttons that are available by default in the TinyMCE 3 install visit the example page here and click the 'view source tab'. There are also tons of plugins available to do all sorts of things.
Adding Buttons
So
lets say for example you wanted to add font color settings to the
editor. All you need to do is add this line to your class or
_config.php file:
So this line will insert the font color button after the format select dropdown in the toolbar. Because font color ships with TinyMCe that's all there is to it! There are literally dozens of extra buttons you can add to your editor in this way, just check out the link above to see all the buttons that ship with TinyMCE.
Here are all the functions for adding buttons:
insertButtonsAfter('ButtonA', 'ButtonB', 'ButtonC', 'ButtonD') - This will insert buttons B, C and D after button A.
insertButtonsBefore('ButtonA', 'ButtonB', 'ButtonC', 'ButtonD') - This will insert buttons 2, 3 and 4 before button A.
addButtonsToLine(1, 'ButtonA', 'ButtonB') - This will add buttons A and B to the end of line 1.
setButtonsForLine( 1, 'ButtonA', 'ButtonB', 'ButtonC') - This will set the entire row of buttons to ButtonsA, B and C.
Removing Buttons
When you want to remove buttons is just as simple as adding them:
This will remove the table controlls buttons which are grouped together in the tinyMCE code.
removeButtons('ButtonA', 'ButtonB') - this will remove buttons A and B from the editor.
Adding / Removing Plugins
If you can't find what your looking for in the inbuilt TinyMCE
controls then you may well be able to find it by using a plugin (or
even write your own). To add a particular plugin add this line of code:
This
will enable the blockquote plugin, but remember you will need to add
the appropriate button separately using one of the methods described
above.
You can also disable plugins using, you guessed it, disablePlugins() which as with enablePlugins requires you to remove the button separately.
enablePlugins('PluginA', '../../customPlugins/pluginB') - this will enable the plugins A and B.
disablePlugins('PluginA', '../../customPlugins/pluginB') - this will disable the plugins A and B.
Setting Options
You can also set the TinyMCE options in the same way, so say I wanted to enable the auto_resize option I would add this code:
You can also set a group of options at once using the setOptions() call.
setOptions(array('Option1' => 'Value1', 'Option2' => 'Value2')) - sets an array of options.
setOptions('Option', 'Value') - sets an option.
Note.
It appears that the settings inside LeftAndMain.php are included after those in _config.php. For this reason if you are trying to change a setting that is set in LeftAndMain.php, you must do it in the getCMSFields() function in your Page.php file.
Special thanks to Martijn van Nieuwenhoven for some suggestions regarding this post.
About the Author
Name: Aram Balakjian
Website: http://www.aabweb.co.uk
Aram is a web designer/developer running London based agency aab web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.
Comments (10)
-
Same problem as Mladen:
I can't find tinymce.template.js I am using silverstripe 2.3.3. I just want to add all buttons in the editor but i can not find out how....Please Help...
Posted by Mladen , 07/09/2009 4:50pm (6 days ago)Posted by felipe, 14/09/2009 4:28am (12 months ago)
-
Hi Mladen
Read the tutorial ;)
AramPosted by aram, 08/09/2009 4:33am (1 year ago)
-
I can't find tinymce.template.js I am using silverstripe 2.3.3. I just want to add all buttons in the editor but i can not find out how....Please Help...
Posted by Mladen , 07/09/2009 4:50pm (1 year ago)
-
Hey your tutorials are really handy!Thanks for putting in the time to write them, you have saved me hours!!
I have a favour to ask... Is there any chance you would be able to show a tutorial on adding buttons using getCMSActions? I\'m really stuck on this and there isn\'t alot of documentation out there about it. :(Posted by Sanchez, 28/08/2009 5:49am (1 year ago)
-
Just curious if anyone know how to customize the options in the format dropdown. I would like to remove h1, address and preformatted.
Posted by Andrew Houle, 07/08/2009 9:43am (1 year ago)
-
Thanks Aram, you\'re the best! :D
Posted by Marcus, 27/07/2009 12:08pm (1 year ago)
-
Hi Marcus
It seems that the settings in LeftAndMain.php are included after those in the _config.php, so if a button is added in that file (e.g. ssimage and tablecotnrols) then removing it in the config wont work. In that case you need to remove it in your getCMSFields function in your Page.php class which then overrides the LeftAndMain settings.
Thanks for pointing this out, I have added a note to the post :)Posted by Aram, 27/07/2009 6:02am (1 year ago)
-
I just wanted to report that
HtmlEditorConfig::get('cms')->removeButtons(');
does not work for me.
I first tried removing the ssimage-button and that didn't work. The example you list here has no effect for me either.
I can add buttons and I have added the ImageManager-plugin like we talked about but for some reason, removeButtons has no effect from the _config-file.Posted by Marcus, 26/07/2009 7:37pm (1 year ago)
-
Thanks Andrew, it is great to hear it's proving useful to people :)
Posted by aram, 26/07/2009 12:46pm (1 year ago)
-
Thanks a bunch guys, I can't wait to try this out!
btw, love the site Aram, it's a fantastic and much needed resource for SilverstripePosted by Andrew Houle, 26/07/2009 8:43am (1 year ago)
« previous 1 2 3 4 next »
RSS feed for comments on this page RSS feed for all comments