This is a long title for a blog post, but it is a complicate and tricky task and I couldn’t find a complete solution, so this is a summary of how I did it. It also provides a good basis for adding other features to TinyMCE inside Drupal. First of all, the versions of the software I was working with were TinyMCE 3.5.4.1 and Drupal 7.14 (yes, we need to upgrade that!) I spent a lot of time hacking inside the Drupal WYSIWYG plugin and inside tinyMCE itself before I discovered the clean plugin-base solution. My starting point was this simple TinyMCE newline Plugin from SYNASYS MEDIA. This didn’t work for me out of the box. I came as only compressed javascript, so I had to figure out how to decompress it first. Once I’d done that, after lots of debugging I worked out that the reason I couldn’t get it to show up inside Drupal is that you have to make a new (minimal) Drupal plugin to register it properly with the WYSIWYG plugin (see below). After that I worked out that they had used ‘<br />’ which didn’t work in all circumstances so I changed it to “<br />\n” which nearly did what I wanted but the cursor got screwed up if you did newline at the end of the text, so I tried adding ed.execCommand(‘mceRepaint’,true); but that didn’t help. I kept looking at the list of mce commandsand spotted “mceInsertRawHTML” but that was worse. In the end I decided to ignore the glitch as it’s purely cosmetic.
My final version is below. I’ve kept the name “smlinebreak” but I’ve bolded it so if you wanted your own name for a plugin you can see where you’d have to tweak it.
(function(){ tinymce.PluginManager.requireLangPack('smlinebreak'); tinymce.create( 'tinymce.plugins.SMLineBreakPlugin', { init:function(ed,url){ ed.addCommand('SMLineBreak',function(){ ed.execCommand('mceInsertContent',true,"<br />\n") }); ed.addButton('smlinebreak',{ title:'smlinebreak.desc', cmd:'SMLineBreak', image:url+'/img/icon.gif' }) }, getInfo:function(){ return{ longname:'Adapted version of SYNASYS MEDIA LineBreak', author:'Christopher Gutteridge', authorurl:'http://users.ecs.soton.ac.uk/cjg/', infourl:'http://www.ecs.soton.ac.uk/',version:"1.0.0"} } }); tinymce.PluginManager.add('smlinebreak',tinymce.plugins.SMLineBreakPlugin)} )();
which replaces the editor_plugin.js in the SMLineBreak I downloaded from http://synasys.de/index.php?id=5. The other files are trivial, just the image for the icon in img/icon.gif and a language file in langs/en.js which looks like
tinyMCE.addI18n('en.smlinebreak',{desc : 'line break'});
This plugin I placed in …/sites/all/libraries/tinymce/jscripts/tiny_mce/plugins/smlinebreak Then I had to register it, not directly with TinyMCE, but rather with the Drupal WYSIWYG plugin, using a custom Drupal module…
Drupal WYSIWYG Plugin
I gave my plugin the catchy title of “wysiwyg_linebreak”. This needs to be inserted into the filenames and function names so I’ll put it inbold for clarity, so you can see the bit that’s the module name. This module gets placed in sites/all/modules/wysiwyg_linebreak/ and has just two files. wysiwyg_linebreak.info is just the bit to tell Drupal some basics about the module. As it’s an in-house hack I’ve not put much effort into it.
name = TinyMCE Linebreaks description = Add Linebreaks to TinyMCE core = 7.x package = UOS
The last line means it gets lumped-in with all my other custom (University of Southampton) modules so they appear together in the Drupal Modules page. The module file itself is wysiwyg_linebreak.module and this is a PHP file which just tweaks a setting to add the option to the Drupal WYSIWYG module.
<?php
/* Implementation of hook_wysiwyg_plugin(). */ function wysiwyg_linebreak_wysiwyg_plugin($editor) { switch ($editor) { case 'tinymce': return array( 'smlinebreak' => array( 'load' => TRUE, 'internal' => TRUE, 'buttons' => array( 'smlinebreak' => t('SM Line Break'), ), ), ); } }
?>
… and that seemed to be enough. To enable it you first need to go into the Drupal Modules page and enable the module, then go to Administration » Configuration » Content authoring » WYSIWYG Profiles and enable the new button in the buttons/plugin section. Then if you’re very lucky it might work.
Summary
It’s possible, even easy, to add new features to the editor inside Drupal. I’ve written this out long form as I couldn’t find a worked example myself of how to add such a feature, and it took me enough time I hope this may give a few short cuts to people needing this or similar features.
Cheers Chris, I’ve been looking for a working example of this for ages. Really helpful.
Thanks a lot Chris!! This was one of the most useful examples I found on the topic.