Last updated July 26, 2011. Created by epimeth on January 13, 2009.
Edited by sun, pillarsdotnet. Log in to edit this page.

This page lists known issues and tutorials for module developers to make their modules compatible to Wysiwyg module.

Expected use of filter_form()

If your module uses an input format enabled textarea, your Form API array structure has to use 'format' as array key to trigger the loading and attachment of a client-side editor. Example:

<?php
 
// Your saved or new data is supposed to have a value and a format. Just like
  // $node has a $node->body and $node->format. May also come from a
  // variable_get('mymodule_admin_setting', array('value' => '', 'format' => NULL));
 
$mydata = mymodule_data_load();
 
$form['myfield']['mytextarea'] = array(
   
'#type' => 'textarea',
   
'#title' => t('My textarea'),
   
'#default_value' => $mydata->value,
  );
 
$form['myfield']['format'] = filter_form($mydata->format);
?>

If your module uses multiple input format enabled textareas in the same form, you still need to use 'format' as array key for each input format selector, but direct filter_form() to use different #parents instead. Admittedly, the proper use of the Form API and filter_form() is not easy to grasp.

Specifically, note the third argument to filter_form(). This will be the key in the $form_state['values'] array once the form is submitted.

Wrong:

<?php
 
// First textarea.
 
$form['field1']['field1'] = array(
   
'#type' => 'textarea',
   
'#default_value' => $myfield1['value'],
   
// ...
 
);
 
$form['field1']['field1_format'] = filter_form($myfield1['format']);
 
// Second textarea.
 
$form['field2']['field2'] = array(
   
'#type' => 'textarea',
   
'#default_value' => $myfield2['value'],
   
// ...
 
);
 
$form['field2']['field2_format'] = filter_form($myfield2['format']);
?>

Correct:

<?php
 
// First textarea.
 
$form['field1']['field1'] = array(
   
'#type' => 'textarea',
   
'#default_value' => $myfield1['value'],
   
// ...
 
);
 
$form['field1']['format'] = filter_form($myfield1['format'], NULL, array('field1_format'));
 
// Second textarea.
 
$form['field2']['field2'] = array(
   
'#type' => 'textarea',
   
'#default_value' => $myfield2['value'],
   
// ...
 
);
 
$form['field2']['format'] = filter_form($myfield2['format'], NULL, array('field2_format'));
...
// In form submission:
 
$myfield1 = array(
   
'value' => $form_state['values']['field1'],
   
'format' => $form_state['values']['field1_format'],
  );
 
$myfield2 = array(
   
'value' => $form_state['values']['field2'],
   
'format' => $form_state['values']['field2_format'],
  );
 
// ...save in whatever way in your form submission handler.
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

One tip that I use:

If you are putting a wysiwyg textarea in a "system settings form", I often use the "#tree" attribute so that the body and format are saved in one variable.

For Example:

function my_settings_form($form_state) {
  $form = array();
  //Get Saved (or default) Body and Format
  $saved = variable_get('my_textarea', array('body' => 'Default Body', 'format' => FILTER_FORMAT_DEFAULT));
  $form['my_textarea'] = array(
    '#type'  => 'item',
    '#tree'   => TRUE,
  );
  $form['my_textarea']['body'] = array(
    '#type' => 'textarea',
    '#title'  => t('My Text Area'),
    '#rows'  =>  8,
    '#default_value' => $saved['body'],
  );
  $form['my_textarea']['format'] = filter_form($saved['format'], NULL, array('my_textarea', 'format'));
  return system_settings_form($form);
}

This should cause the body and format to be saved to a 'my_textarea' variable as an array. In my opinion, this is easier to work with and makes printing the results very easy to read.

For example:

   //Get Saved Values
  $my_textarea = variable_get('my_textfield', array('body' => 'Default Body', 'format' => FILTER_FORMAT_DEFAULT));
  print check_markup($my_textarea['body']. $my_textarea['format'], FALSE);

Since Drupal 7 has no filter_form() function. Anybody could help me integrate Wysiwyg in D7? It seems that filter_process_format is the way to go but I don't know how to use that to achieve the same result with filter_form().

Did you ever figure out how to integrate this module with Drupal 7?
I've search everywhere high and low and there seems to be no information on how to do it.

+1 for that question because it seems that setting the right "#text_format" ID of the FAPI element doesn't trigger the wysiwyg editor to be included unless it's an "edit node" form.

Created subpage for Drupal 7 module integration.

Good. — Fast. — Cheap.
(Pick any two.)

I've implement what's on the subpage but I don't get any of the GUI for my WYSIWYG editor. I just get the option to change text formats. Is there something else I should be adding for the GUI to show as well?

Thanks
Paul

Enable and configure the Wysiwyg module. If it still doesn't work, post a support request to their issue queue.

Good. — Fast. — Cheap.
(Pick any two.)