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.

  1. /**
  2.  * Customize a TinyMCE theme.
  3.  *
  4.  * @param init
  5.  * An array of settings TinyMCE should invoke a theme. You may override any
  6.  * of the TinyMCE settings. Details here:
  7.  *
  8.  * <a href="http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm<br />
  9. " title="http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm<br />
  10. ">http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm<br />
  11. </a> *
  12.  * @param textarea_name
  13.  * The name of the textarea TinyMCE wants to enable.
  14.  *
  15.  * @param theme_name
  16.  * The default tinymce theme name to be enabled for this textarea. The
  17.  * sitewide default is 'simple', but the user may also override this.
  18.  *
  19.  * @param is_running
  20.  * A boolean flag that identifies id TinyMCE is currently running for this
  21.  * request life cycle. It can be ignored.
  22.  */
  23. function phptemplate_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
  24. switch ($textarea_name) {
  25. // Disable tinymce for these textareas
  26. case 'log': // book and page log
  27. case 'img_assist_pages':
  28. case 'caption': // signature
  29. case 'pages':
  30. case 'access_pages': //TinyMCE profile settings.
  31. case 'user_mail_welcome_body': // user config settings
  32. case 'user_mail_approval_body': // user config settings
  33. case 'user_mail_pass_body': // user config settings
  34. case 'synonyms': // taxonomy terms
  35. case 'description': // taxonomy terms
  36. case 'body': // taxonomy terms
  37.  
  38. // For node body fields, break if not a newsletter
  39. if (textarea_name == 'body') {
  40.  
  41. // APPEND THIS ARRAY TO DISABLE ADDITIONAL NODE TYPES
  42. $disabled_types = array('newsletter');
  43.  
  44. // Grab a clean nid from the url
  45. $nid = check_plain(arg(1));
  46.  
  47. // Query for the node type
  48. $node = db_fetch_object(db_query("SELECT type FROM {node} WHERE nid=%d", $nid));
  49.  
  50. // Break out if the node type doesn't existin the array
  51. if (!in_array($node->type, $diabled_types)) {
  52. break;
  53. }
  54. }
  55.  
  56. unset($init);
  57. break;
  58.  
  59. // Force the 'simple' theme for some of the smaller textareas.
  60. case 'signature':
  61. case 'site_mission':
  62. case 'site_footer':
  63. case 'site_offline_message':
  64. case 'page_help':
  65. case 'user_registration_help':
  66. case 'user_picture_guidelines':
  67. $init['theme'] = 'simple';
  68. foreach ($init as $k => $v) {
  69. if (strstr($k, 'theme_advanced_')) unset($init[$k]);
  70. }
  71. break;
  72. }
  73.  
  74. /* Example, add some extra features when using the advanced theme.
  75.  
  76.   // If $init is available, we can extend it
  77.   if (isset($init)) {
  78.   switch ($theme_name) {
  79.   case 'advanced':
  80.   $init['extended_valid_elements'] = array('a[href|target|name|title|onclick]');
  81.   break;
  82.   }
  83.   }
  84.  
  85.   */
  86.  
  87. // Always return $init
  88. return $init;
  89. }