Disable TinyMCE in Node Body Based on Node Type

There's been a few sites I've used WYSIWYG editors on recently where I've run into the issue of needing to disable it on certain text areas. TinyMCE supports this with a theme override just fine, however, it's not so simple when you have multiple text areas with the same name.

Take the node edit form. For every node type, the body uses the same name.

A practical example of when you would need to disable a WYSIWYG editor would be when filling a node body with previously designed HTML like when you design an HTML Newsletter.

The solution is not that difficult, although requires an extra query.

In the code below, I took the theme function from TinyMCE named theme_tinymce_theme(), renamed it to phptemplate_tinymce_theme() and put it in my template.php file.

I then added body to the case statement and added the conditional logic below. You can see the only node type I'm disabling is newsletter, however if you wish to replace that, or add others, you can simply append the array.

/** * 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 phptemplate_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {  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 'body': // taxonomy terms      // For node body fields, break if not a newsletter      if (textarea_name == 'body') {        // APPEND THIS ARRAY TO DISABLE ADDITIONAL NODE TYPES        $disabled_types = array('newsletter');        // Grab a clean nid from the url        $nid = check_plain(arg(1));                // Query for the node type        $node = db_fetch_object(db_query("SELECT type FROM {node} WHERE nid=%d", $nid));                // Break out if the node type doesn't existin the array        if (!in_array($node->type, $diabled_types)) {          break;        }      }      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;  }  /* Example, add some extra features when using the advanced theme.  // If $init is available, we can extend it  if (isset($init)) {    switch ($theme_name) {     case 'advanced':       $init['extended_valid_elements'] = array('a[href|target|name|title|onclick]');       break;    }  }  */  // Always return $init  return $init;}