How to disable TinyMCE per fields in Drupal

There is a theme() function in the Drupal tinymce module that lets you (the admin/webmaster) turn off the rich text editor on a field by field basis.

Basic steps to disable TinyMCE in a specific Drupal form field:

  1. Look in your modules/tinymce/tinymce.module file.
  2. Search for "theme_tinymce_theme".
  3. Copy the entire function to your theme's template.php file.
  4. Rename the function from theme_tinymce_theme() to yourthemename_tinymce_theme(). (Substitute "yourthemename" for your theme's name.)
  5. Add a line to the switch($textarea_name){} block that specifies the name of the textarea you would like to exclude. Use the existing examples as a guide.

I wanted to disable the RTE for a custom plain text field in the node edit form. The field's name is "field-teaser-0-value". This is the line I added (marked with "KA") with some surrounding logic: (The fully modified theme that I put into my template.php is at the end of the post.)

 

function theme_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
switch ($textarea_name) {
case 'field-teaser-0-value': // KA: blog teaser (we want plain text only)
unset($init);
break;
}
}

 

To find the name of the field in question, load the form in your browser. Check the source code. Look for the name="" attribute for your field. That's the string you insert into the theme function.

Tip! For CCK (content) fields where the name is something like "field_teaser[0][value]", you need to replace the non-alphanumeric characters with a dash. So the $textarea_name variable for this field is actually written as "field-teaser-0-value".

Here's the final customized theme function I placed in my theme's template.php file. My theme is called "avocadoshake" and my comments are marked with a "KA".

/**
* Customize a TinyMCE theme.
*
* @param init
* An array of settings TinyMCE should invoke a theme. You may override any
* of the TinyMCE settings. Details here:
*
* http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
*
* @param textarea_name
* The name of the textarea TinyMCE wants to enable.
*
* @param theme_name
* The default tinymce theme name to be enabled for this textarea. The
* sitewide default is 'simple', but the user may also override this.
*
* @param is_running
* A boolean flag that identifies id TinyMCE is currently running for this
* request life cycle. It can be ignored.
*/
function avocadoshake_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
// KA: uncomment this print line (on a dev site!) to print out all the textarea names on a form
// print "textarea_name = $textarea_name <br />";
switch ($textarea_name) {
// Disable tinymce for these textareas
case 'log': // book and page log
case 'img_assist_pages':
case 'caption': // signature
case 'pages':
case 'access_pages': //TinyMCE profile settings.
case 'user_mail_welcome_body': // user config settings
case 'user_mail_approval_body': // user config settings
case 'user_mail_pass_body': // user config settings
case 'synonyms': // taxonomy terms
case 'description': // taxonomy terms
case 'workflow_comment': // workflow state comment
case 'field-teaser-0-value': // KA: blog teaser (plain text only)
unset($init);
break;

// Force the 'simple' theme for some of the smaller textareas.
case 'signature':
case 'site_mission':
case 'site_footer':
case 'site_offline_message':
case 'page_help':
case 'user_registration_help':
case 'user_picture_guidelines':
$init['theme'] = 'simple';
foreach ($init as $k => $v) {
if (strstr($k, 'theme_advanced_')) unset($init[$k]);
}
break;
}
}

 

Comments

Post new comment

Please keep the comments civil and on-topic. Abusive or inappropriate comments will be removed without warning.

The content of this field is kept private and will not be shown publicly.
If you leave a link (include the http:// part), your name will be linked to your homepage.

You may use some HTML for formatting: <strong>bold text</strong>, <em>italics</em>, and <a href="">for links</a>. Empty lines are automatically converted to paragraph breaks.

Or click the link above that says 'enable rich-text' to use the fancy editor.

Captcha
This question is for testing whether you are a human visitor and to prevent automated spam submissions.