Enable WYSIWYG for a custom textarea form field in drupal

13,608

Solution 1

First, you should not create forms like that. Instead, use Drupal's built-in Forms API. Take a look at the Form API Quickstart Guide for more information.

This page on Drupal's site will help you add WYSIWYG to your custom forms: http://drupal.org/node/358316

Solution 2

Well, how ever you create forms is up to you, it just depends if it has ever come back to bite you in the butt...

Look at the comment module. I had noticed that it was possible to choose input format for comments, and when Full/filtered HTML was selected, the WYSIWYG editor kicked in. Here is the related code:

$form['comment_filter']['comment'] = array(
  '#type' => 'textarea',
  '#title' => t('Comment'),
  '#rows' => 15,
  '#default_value' => $default,
  '#required' => TRUE,    

);
if (!isset($edit['format'])) {
  $edit['format'] = FILTER_FORMAT_DEFAULT;
}
$form['comment_filter']['format'] = filter_form($edit['format']);

So, you define an array with two elements, one of which is the textarea itself, and the other one the format chooser generated by filter_form, and that's all.

This was from http://groups.drupal.org/node/104604

Solution 3

For D7, its even simple. We got new type called text_format.

$form['comment'] = array('#type' => 'text_format', 
                         '#format' => 'full_html', 
                         '#title' => t('Description'), 
                         '#required' => TRUE);

Solution 4

Yes, you can do this. Just add the following javascript to your page.

<script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
        tinymce.init({selector:'textarea'});
</script>

This script automatically converts the text areas in your html code to rich text editors

More info at this link.

Share:
13,608
shasi kanth
Author by

shasi kanth

I am a Web Developer, Husband, Father and Singer. [I am #SOreadytohelp] When not doing work, I would spend time with meditation and music. My Developer Story

Updated on June 04, 2022

Comments

  • shasi kanth
    shasi kanth almost 2 years

    i have created a custom form in my custom drupal6 module and the form has a textarea. I am outputting the form as html embedded inside the php code, like the following:

    function custom_my_form() 
    {
    $f = '<form name="add_user" action="" method="post">
    <label>  Name </label>
    <p><input type="text" name="name" value="" /></p>
    <label>About Yourself</label>
    <p><textarea name="desc"></textarea></p>
    <input type="submit" name="submit" value="Add">
    </form>';
    
    return $f;
    }
    

    I have installed and enabled the WYSIWYG module. My default input format is "Filtered HTML" and i have chosen the FCK editor for this input format.

    Now i want to enable the WYSIWYG for the textarea field in this form. How can i go ahead with this ?