19

I'm attempting to give my content editors the ability to change a variable for a custom module. This variable can have html. I don't want to require them to have any knowledge of HTML, so I want to give them the ability to have our wysiwyg (CKEditor provided by the CKEditor module) at their disposal. However, as of now, the data does not save as a variable. I've read in a few places (D.o, stackexchange) that I needed to do something like this:

  $form['foo']['bar'] = array(
    '#type'     => 'text_format',
    '#title'    => 'Bar',
    '#definition' => 'Foo Bar',
    '#default_value' => variable_get('bar', ''),
    '#weight'   => 1,
    '#format' => 'filtered_html',
    '#required' => FALSE,
  ); 

This does not work out-of-the-box with a system_settings_form($form). A var_dump shows this after a save:

array (size=7)
  '#type' => string 'text_format' (length=11)
  '#title' => string 'Bar' (length=7)
  '#definition' => string 'Foo Bar' (length=74)
  '#default_value' => 
    array (size=2)
      'value' => string '' (length=0)
      'format' => string 'filtered_html' (length=9)
  '#weight' => int 1
  '#format' => string 'filtered_html' (length=9)
  '#required' => boolean false

From this, you can see that 'format' saves correctly, but 'value' does not. What am I missing?

Also, if there is a better way to do this, please do tell.

28

The following worked for me; the #default_value is expecting a string but the text_format form element is stored as an array.

$barvalue = variable_get('bar', array('value' => '', 'format' => NULL));

$form['foo']['bar'] = array(
'#type'     => 'text_format',
'#title'    => 'Bar',
'#definition' => 'Foo Bar',
'#default_value' => $barvalue['value'],
'#weight'   => 1,
'#format' => $barvalue['format'],
'#required' => FALSE,
); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.