background top

Disabling WYSIWYG on comment field in Drupal 6

Integration of wysiwyg editors into Drupal continues to evolve and we can expect to see even more improvements in Drupal 7. In Drupal 6, the TinyMCE module was discontinued and the WYSIWYG module has taken its place. The WYSIWYG module works as an api for integrating editors such as TinyMCE, FCKeditor, markItUp, NicEdit and Whizzywig.

One important feature that WYSIWYG lacks is the ability to exclude certain form fields. We ran into this issue when we wanted to disable wysiwyg formatting on the comments field. There had been suggestions here and here on the drupal forums about setting a #wysiwyg attribute; this we tried with no success. Another suggestion was to use the Better Formats module, but the thought of adding yet another module for something so simple wasn’t very appealing.

In the end, we ended up writing a small amount of code to achieve this. We added the following function to our custom module. What we’re doing here is removing the ‘format’ entirely from the comments field. If you needed to alter a different form, replace comment-form with the id of your form and comment_filter with your field. You may need to try a print_r($form_id) to find out the id of your form item.

function my_module_form_alter($form_id, &$form) {
  if ($form_id['#id'] == 'comment-form') {
    $form_id['comment_filter']['format'] = array();
  }
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • TwitThis

17 Responses to “Disabling WYSIWYG on comment field in Drupal 6”

  1. Matt Says:

    Thanks for the post, this helped a lot. Do you know why this works? From what I understand, all your doing here is removing the radio buttons for the format options from the form. Why does this also disable the TinyMCE editor?

    Great fix.

    Thanks,
    ~Matt

  2. Noah Says:

    Hi Matt. Glad you found this post helpful.

    Since the input formats are role-based in Drupal, you would normally see whichever ones your role is entitled to, on all textarea fields. The radio button options are built from the format array itself, which is why altering the form works. Replacing the format with an empty array causes Drupal to use the built-in format, as if the user has no input formats available at all, but only for whichever field and form you specify.

  3. Jeff Geerling Says:

    I’m trying to get this to work by simply creating a dummy module for it. The contents of my module are here: http://drupalbin.com/10162 . What am I doing wrong? It’s still leaving WYSIWYG on all the comment forms :-(

  4. Merakli Says:

    I created a custom module named ‘commentform’ and put the function above into commentform.module

    enabled the module.

    Comment forms are still showing wysiwyg (tinyMCE)

    I tried changing ‘my_module_form_alter’ to ‘commentform_form_alter’, disabled and re-enabled the module but that didn’t help either.

    What am I missing?

    Thanks

  5. jaan Says:

    how do i create a custom module? can you upload the file(s) we need to disable the wysiwyg editor for comments?

    cheers

  6. kurt graux Says:

    A question on the intergration of this function: where do you put this code exactly?

    I pasted the function into page.tpl.php, but that’s not the way to do it, it seems, although the id of the comment field is indeed comment-form…

  7. Noah Says:

    Merakli, I’m not sure what might be missing from your module. Try outputting some test code in your module to make sure it’s loading correctly. Something like this would help you see if Drupal is getting into your function:

    function commentform_form_alter($form_id, &$form) {
    if ($form_id['#id'] == 'comment-form') {
    echo 'in comment form';
    die();
    }
    

    If that doesn’t work then your form may have a different id. Take a look at the page source to verify that. Also, watch those quotes if you’re copying and pasting from the browser.

  8. Noah Says:

    jaan and kurt:
    I created a new module, which includes two files: my_module.info and my_module.module. The .info file needs the required fields described here. As for the actual code, the .module file contained the function written above. Make sure that your function name is derived from your module name.

    For detailed instructions on module development, go here.

  9. Tristan Says:

    Can you PLEASE post an example module file? I tried my best, but still have that damn WYSIWYG there. I like it for writing posts, but HATE it for the comments due to security issues that can come up.

  10. Garry Says:

    Hi Noah,

    Would you have to have seperate code for each page that your comment form would appear on? Or does the comment form have a universal ID that is the same on every page that it appears on?

    Cheers, Garry.

  11. Alex Dicianu Says:

    You can also use this Drupal module: http://drupal.org/project/better_formats
    It’s much more simpler this way.

  12. Liz Says:

    Kurt, you might try what I did in my theme… I didn’t have to make a whole new module for this. it doesn’t pass in $form_id .

    In a php file in my theme, I have a function for theme_comment_form to suppress some field titles from printing before their textareas. It looks something like this:

    function theme_comment_form($form) {
    $form['comment_filter']['format'] = array();
    }

  13. Nitin garg Says:

    Hi All,

    Just replace the line text in comment.module in comment module
    near about 1382 for me-

    - $form['comment_filter']['format'] = filter_form($edit['format']);
    + $form['comment_filter']['format'] = array();

    have a nice day and enjoy

    no need to do any thing else

  14. Andy Says:

    If you have made a module, the code in the post didn’t work for me. Instead I used:

    function MODULENAME_form_alter(&$form, $form_state, $form_id) {
    if ($form_id == ‘comment_form’) {
    $form['comment_filter']['format'] = array();
    }
    }

    Andy

  15. Kawika Says:

    The custom module code worked for me. Thanks!

  16. Ecommerce Canada | Asciicart Says:

    It works for me as well… Thanks mate

  17. Triactol Review Says:

    Thank you, it work good for me to :D

Leave a Reply