Developer info (D6-)

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

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:

  // 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:

  // 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:

  // 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.

Help improve this page

Page status: Not set

You can: