Developer info

Last modified: June 20, 2009 - 23:54

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
  $form
['myfield']['mytextarea'] = array(
   
'#type' => 'textarea',
   
'#title' => t('My textarea'),
   
'#default_value' => mymodule_get_myfield_value(),
  );
 
$form['myfield']['format'] = filter_form(mymodule_get_myfield_format_value());
?>

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',
   
// ...
 
);
 
$form['field1']['field1_format'] = filter_form($field1_format);

 
// Second textarea.
 
$form['field2']['field2'] = array(
   
'#type' => 'textarea',
   
// ...
 
);
 
$form['field2']['field2_format'] = filter_form($field2_format);
?>

Correct:

<?php
 
// First textarea.
 
$form['field1']['field1'] = array(
   
'#type' => 'textarea',
   
// ...
 
);
 
$form['field1']['format'] = filter_form(mymodule_get_field1_format_value(), NULL, array('field1_format'));

 
// Second textarea.
 
$form['field2']['field2'] = array(
   
'#type' => 'textarea',
   
// ...
 
);
 
$form['field2']['format'] = filter_form(mymodule_get_field2_format_value(), NULL, array('field2_format'));

...
...
...
...

 
//In form validation / submission
 
mymodule_set_field1_format_value($form_state['values']['field1_format']);
 
mymodule_set_field2_format_value($form_state['values']['field2_format']);
?>

Incomprehensible example

sbayne - May 15, 2009 - 21:25

These examples show a $form array that is deeper that such an array normally is, without defining what the first level actually is.
So on a form with a textarea, I would expect $form['fieldname'] = array('#type' => 'textarea',//...). These examples show $form[??????]['fieldname'].

This works great as comprehensible example

Stalski - June 9, 2009 - 18:00

 
  $form['accepted_text']['accepted_text'] = array(
    '#type' => 'textarea',
    '#title' => t('Message on success'),
    '#default_value' => variable_get('accepted_text', ''),
    '#description' => t('Text to let users know ...'),
  ); 
  $form['accepted_text']['format'] = filter_form(2, NULL, array('accepted_text_format'));
 
  $form['teaser_text']['teaser_text'] = array(
    '#type' => 'textarea',
    '#title' => t('Message for quick overview block'),
    '#default_value' => variable_get('teaser_text', ''),
    '#description' => t('Text to show the most important things.'),
  ); 
  $form['teaser_text']['format'] = filter_form(2, NULL, array('teaser_text_format'));
 
  $form['short_text']['short_text'] = array(
    '#type' => 'textarea',
    '#title' => t('Message for quick overview block'),
    '#default_value' => variable_get('short_text', ''),
    '#description' => t('Text to show what this is all about.'),
  );
  $form['short_text']['format'] = filter_form(2, NULL, array('short_text_format'));

Maybe I'm misunderstanding

pmckibben - June 27, 2009 - 00:03

Maybe I'm misunderstanding something, but it appears that Stalski's example hard-codes the input format selection and does not ever save/retrieve it.

The first example actually makes sense to me. Perhaps the confusion is around how PHP arrays work?

Perhaps the confusion is how to write the functions mymodule_get_field1_format_value(), mymodule_set_field1_format_value(), etc. There are many ways you can do that, but it could be as simple as getting/setting a variable. I would replace those functions as follows:

mymodule_get_field1_format_value(): replace with variable_get('mymodule_field1_format', FILTER_FORMAT_DEFAULT)
mymodule_set_field1_format_value(): replace with variable_set('mymodule_field1_format', $form_state['values']['field1_format'])

Paul McKibben
Drupal Consultant in Atlanta, GA (USA)
Principal, Digital Valence

Personal: http://paulmckibben.com
Company: http://www.digital-valence.com

Can you make it clearer

yookoala - September 1, 2009 - 07:33

If I'm to write this "mymodule", how should I write this mymodule_get_myfield_format_value() function?
What should it returns?

I think it'd be a lot clearer if there is example that's work.
I know it might change with my module, but it'd be still better if I get to know some useful case.
For example, what should mymodule_get_myfield_format_value() if "myfield" is a HTML field?

I can't get my code to work

yookoala - September 1, 2009 - 09:55

I wrote these:

<?php
  $form
['body']['body'] = array(
   
'#type' => 'textarea',
   
'#title' => check_plain($type->body_label),
   
'#default_value' => $node->body,
   
'#required' => FALSE,
   
'#row' => 4,
   
'#resizable' => FALSE,
  );
 
$form['body']['filter'] = filter_form($node->format);
 
$form['body']['format'] = filter_form(2, NULL, array('body_format')); // to integrate wysiwyg
?>

And I'm using FCKeditor.
My browser then pop-up this box with javascript:

Error: The TEXTAREA with id or name set to "" was not found

Am I writing anything wrong?

+1 I'm having the same

btopro - September 1, 2009 - 17:19

+1 I'm having the same trouble understanding fully how this will get integrated.

"Plaguing the world with Drupal; One Plone, Moodle, Wordpress, Joomla user at a time since 2005." ~ btopro

http://elearning.psu.edu/
http://elearning.psu.edu/projects/
http://elearning.psu.edu/drupalineducation/

Try this

TwoD - September 10, 2009 - 20:55

<?php
  $form
['body']['body'] = array(
   
'#type' => 'textarea',
   
'#title' => check_plain($type->body_label),
   
'#default_value' => $node->body,
   
'#required' => FALSE,
   
'#row' => 4,
   
'#resizable' => FALSE,
  );
 
$form['body']['format'] = filter_form($node->format, NULL, array('body_format')); // to integrate wysiwyg
?>

filter_form() generates the entire fieldset (or just the last part if there's just one format) for selecting the active format, so no need to use it twice.

For System Settings Forms

te-brian - October 3, 2009 - 20:15

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

 
 

Drupal is a registered trademark of Dries Buytaert.