Merveilles du web 2.0… mon « copier bloguer » du web

22 août 2008

How to disable TinyMCE per fields in Drupal | avocado shake (dot) net

Filed under: drupal — elrems @ 11:07

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;
  }
}

 

How to disable TinyMCE per fields in Drupal | avocado shake (dot) net

Blogged with the Flock Browser

Tags: ,

1 Commentaire »

  1. Thank you very much!

    Commentaire par Online hry — 4 septembre 2008 @ 13:51 | Répondre


Flux RSS des commentaires de cet article. URI du rétrolien

Répondre

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Twitter picture

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Connexion à %s

Thème : Rubric. Un Blog WordPress.com.

Suivre

Get every new post delivered to your Inbox.