Lullabot Ideas

We know stuff. We empower you to know stuff too.

WYSIWYG as a Feature

Article by Karen Stevenson

One of the things Drupal core does not do well at all is provide an easy way to switch on a WYSIWYG editor. There is no editor out of the box and setting it up requires some custom configuration of several Drupal core settings, installing and configuring several contributed modules, and also installing one or more external libraries, like the TinyMCE library. The Lullabot book, Using Drupal (O'Reilly), devotes a whole section to describing one way to configure a WYSIWYG editor.

The Problems

It's complicated because there are lots of inter-related parts:

  1. You need to set up one or more roles that can edit content.
  2. You need to create and configure 'Input formats' to create at least one format that allows users to add the html and css needed to create rich text content.
  3. You need to select and install a WYSIWYG editor: install the editor, download an appropriate javascript library and move it to the right location (which varies depending on which editor you are using), and then configure the editor to use the library.

And all of that gets you only to the point where you can edit text. If you want to allow users to upload images and insert them into their text, you also need to identify which of several possible methods of image handling you want to use, and install and configure the modules needed to manage that.

If you've never set a rich text editor up in Drupal before, it can take quite a long time to figure out what to do and do it. Even if you've done it before it typically involves lots of time to navigate to all the needed configuration pages and set each module up appropriately. And once you've done it on one site, there's no easy way to replicate the setup on another site.

So there is a real need for a way to pre-configure as much of this as possible to make it easier to set up and deploy this to other sites, something we can do using the Features module.

WYSIWYG Options

There are lots of possible ways to set something like this up. For the editor you could use a module like FCKEditor or TinyMCE, or do this the way many of us are doing it now, using the WYSIWYG module and configuring it to use the library of our choice.

For image handling there are numerous options: you could use the Image module to create nodes for each of the uploaded images, or you could use the Imagefield module along with helpers like the Insert module and Filefield Sources to attach images to nodes as CCK fields, or you could use the core Upload module to attach images to nodes without making them into fields or nodes, or use IMCE or Image Browser to create 'loose' images that are not attached to nodes at all. Most of these methods work nicely with the Imagecache module, which makes it possible to create a collection of preset sizes for your images.

Gathering the Modules and Files

An extensive discussion of all the possible kinds of image handling and all the other things you can do with Imagecache is an article (or two) in itself. So without delving into all the reasons for choosing one of these methods over another we're going to build a feature that uses the WYSIWYG module and the TinyMCE library as an editor, and the Image Browser and Imagecache for image handling.

And we're going to add a couple other handy additions: Image Resize Filter so we can grab the image that has been dropped into our text area to make it bigger or smaller, and Transliteration, a helper module that ensures that the names of the files that we create don't have spaces or other invalid characters in them. And Image Browser requires that we have Views and jQuery UI installed. Note that we want version 2 of Image Browser, not version 1.

We also need the modules that make Features work, Features, Strongarm, and CTools, along with a new module called Input Formats that lets us import and export input formats.

Finally we need the WYSIWYG module with a patch at http://drupal.org/node/624018 that makes it possible to import and export WYSIWYG configuration, and the TinyMCE library.

If we express this in a make file, it looks like this:

; DRUPAL VERSION
core = 6.x

; CORE MODULES
projects[] = drupal
projects[views][subdir] = "contrib"

; FILE HANDLING
projects[imagecache][subdir] = "contrib"
projects[imageapi][subdir] = "contrib"
projects[transliteration][subdir] = "contrib"
projects[image_resize_filter][subdir] = "contrib"
projects[imagebrowser][subdir] = "contrib"
projects[imagebrowser][version] = '2.x-dev'

; FEATURES
projects[features][subdir] = "contrib"
projects[strongarm][subdir] = "contrib"
projects[ctools][subdir] = "contrib"
projects[input_formats][subdir] = "contrib"

; WYSIWYG
projects[wysiwyg][subdir] = "contrib"
; Add a patch to make wysiwyg exportable.
projects[wysiwyg][patch][] = "http://drupal.org/files/issues/wysiwyg-624018-ctools-export-input-formats-2.patch"

; LIBRARIES
projects[libraries][subdir] = "contrib"
projects[jquery_ui][subdir] = "contrib"

; TinyMCE
libraries[tinymce][download][type] = "get"
libraries[tinymce][download][url] = "http://downloads.sourceforge.net/project/tinymce/TinyMCE/3.2.7/tinymce_3_2_7.zip"
libraries[tinymce][directory_name] = "tinymce"

; jQuery UI
libraries[jquery_ui][download][type] = "get"
;libraries[jquery_ui][download][url] = "http://jquery-ui.googlecode.com/files/jquery-ui-1.7.3.zip"
libraries[jquery_ui][download][url] = "http://jquery-ui.googlecode.com/files/jquery.ui-1.6.zip"
libraries[jquery_ui][directory_name] = "jquery.ui"
libraries[jquery_ui][destination] = "modules/contrib/jquery_ui"

Creating a Feature

After this we can set up a site the old-fashioned way, by installing all the necessary modules and libraries, creating the imagecache presets, creating the input filters, setting up user permissions (including permissions to view all the imagecache presets and to use them in the Image Browser), setting up the WYSIWYG editor and choosing all the buttons and options you want it to use.

Now the magic happens. We go to the Features page and choose the option to create a new feature. Into the feature we add the WYSIWYG and input filter settings, the Imagecache presets, and all the related permissions and variables. Then we download the feature and add it to another site.

Viewing the Results

I have attached a copy of the feature this created. You should be able to try it out by creating a new Drupal installation that has all the required modules and libraries (you can create one with the make file and Drush Make). Then add the WYSIWYG Feature code to the site, go to the Features page, and enable it. You will need to clear the caches after enabling it and then you should be able to see that WYSIWYG and the input filters and the Imagecache presets and the permissions are all set up correctly. Try to create a new story and the WYSIWYG editor should appear.

Keep in mind that the WYSIWYG patch is brand new, as is Version 2 of Image Browser, so there are probably still a few hiccups in this process. But the take-away is that it is quickly becoming possible to lock WYSIWYG functionality in code and deploy it to other sites.

AttachmentSize
feature_wysiwyg.zip5.19 KB
feature_wysiwyg.make_.txt1.41 KB

Comments

wysiwyg_imageupload as an alternative

Hi,

there is an alternative to IMCE / imgassist / Image browser called wysiwyg_imageupload. Its main use case is to be very easy to use ( 2 clicks for an image), supporting imagecache, lightbox, meta fields and an API to extend the meta fields using whatever you like ( like using EXIF data, desription, author, licence or whatever you like). It is based pon the WYSIWYG api, though supports nunbers of editors like CKeditor, FCKeditor, TinyMCE, BUIeditor and the others supported by the wysiwyg api.

It does not sue nodes as a base entitiy for an image, what speeds up the filter a lot and it support filter caches. You still are able to use a submodule to have a image<->node realtion, e.g. for commenting images and similar things.

You have several API hooks and a number of theme methods to change the look and feel or extend the features.

The core module is kept slim, e.g. lightbox or the imagebrowser are submodules and can be installed optionally. Same goes for the mirgation modules and for the views integration.

Good to know

Interesting, I will try this out sometime. As I said, this wasn't intended as an article about all the ways you can do image handling. There are numerous other options besides the ones I listed.

Upload should be included as

Upload should be included as a dependency, otherwise the image browser part won't work.

Oh and I forgot to say thank

Oh and I forgot to say thank you :) Had this set up in literally no time thanks to your drush make file.

Upload is not a dependency

I do not even have the upload module enabled, it works fine without it. Maybe you have the wrong version of Image Browser, I am using version 2.

Well it turns out that it

Well it turns out that it does work when I deactivate upload module, but the imagecache presets didn't get created and nothing happened when I clicked on the image browser icon in the WYSIWYG. I used your make file so I should have the same versions, right?

I tried a second installation from the make file. After installing I only activate your feature. I get a message saying "Please enable Image API and one of it's toolkits first.". I create content and nothing happens when I click the image browser icon. I then returned to the modules page to activate Upload. Then this message shows up:

* ImageCache preset 1: ib_thumbnail and corresponding actions saved.
* The configuration options have been saved.
* Styles cache updated.

And now I can use the image browser icon.

Clear caches

Right, you have to clear the caches after installing. That is mentioned in the article, but could be missed.

If I create a new installation, enable all the modules, then go to admin/configure/performance and clear the caches, it works fine for me.

That makes sense, duh!

That makes sense, duh!

WYSIWYG Filter and Better Formats

Could these two modules be added (WYSIWYG Filter and Better Formats) to a feature like this? I find that I often use them to both give advanced users more control with the editor, and at the same time use better formats to restrict what untrusted users can do.

Wysiwyg Filter settings can

Wysiwyg Filter settings can be exported in a feature using Input Formats module.

Better formats doesn't have export capabilities yet. But here is the issue with an incomplete patch: http://drupal.org/node/616496

sweet!

Just tried this and it looks great. SO MUCH EASIER to just grab the feature than setting everything up manually.

Thanks!

ib_wysiwyg?

Hi, I'm testing out this feature, but I can't figure out what the ib_wysiwyg dependency is. Can anyone help me? Google isn't pulling up anything useful.

How to get ib_wysiwyg

Download the most recent dev version of ImageBrowserhttp://ftp.drupal.org/files/projects/imagebrowser-6.x-2.x-dev.tar.gz, as of this writing. ib_wysiwyg is included. Good luck!

Problem with recreating this from scratch

Hi,

Thanks to Fureigh above for pointing me to the right place with the ib issue.

But what i want to do is make this, but without the image support. But when I go to recreate it, or make it from scratch (already have all the modules, and setup) I don't see the feature options that I see in the image shown above in the article. For example I have no "Wysiwyg Profiles" option in "Create Feature." Nor do I see anything about roles. Can anyone suggest what i'm doing wrong, or need to do in order to see that?

Thanks!

Got it

Missed that Karen said there was a needed patch on wysiwyg module. So I'm all set now.

exportables missing from the makefile?

I think exportables is missing from the makefile.
2.x?

Yeah I think so too.

Yeah I think so too.

feature overridden?

I did get the patch and patch wysiwyg:
patch -p0 < wysiwyg-624018-ctools-export-input-formats-2.patch
patching file wysiwyg.admin.inc
patching file wysiwyg.features.inc
patching file wysiwyg.install
patching file wysiwyg.module

I also cleared the cache.

But when I go to the features admin page, it lists the feature as overridden.

The part that is overridden is the Wysiwyg Profiles, 1 and 2.
And using revert does not help.

So, I'm wondering if maybe I got the wrong version of the wysiwyg patch.

Input Formats module version

Thanks for the great article Karen!

I used your attachments to try it myself. It worked well with one exception. I think a recent version of Input Formats module broke something along the way. I rolled back to the version that matched the date of this article and it worked!

gist

Boris posted this as a gist on github so it's easily forkable!

http://gist.github.com/456152

tinymce and image browser

Hi!

I just tried it and tinymce does not work with image browser. The page was already set up with insert+image resize. I installed manually everything (no drush), set permissions etc. When I went to Features it was already enabled, and the state was "overridden".
Now I create content, tinymce shows its full html interface (although filtered html is selected) and clicking on the image button does not invoke image browser.
What am I doing wrong?

panos

re: tinymce and image browser

Hello Panos,

1. Ensure you have Image Browser 2 (dev version). If you have Image Browser 1 you must uninstall (1st. disable module, then admin -> site building -> modules -> uninstall), delete the directory and re-install version 2.
2. Enable Image Browser Module and Image Browser WYSIWYG plugin.
3. Go to WISYWYG config (admin -> site config -> WYSIWYG), edit your the input format of your choice, under the "buttons and plugins" group, enable Image Browser.
4. I think you also need to define an ImageCache preset (admin -> build -> ImageCache): add new preset.

That's all.