Drupal 7 introduces the form element #type text_format, which is a text-format-enabled version of a textarea.

  // Retrieve the default values for 'value' and 'format', if not readily
  // available through other means:
  $defaults = array(
    'value' => '',
    'format' => filter_default_format(),
  );
  $my_richtext_field = variable_get('my_richtext_field', $defaults);

  // Just construct a regular #type 'text_format' form element:
  $form['my_richtext_field'] = array(
    '#type' => 'text_format',
    '#title' => t('My richtext field'),
    '#default_value' => $my_richtext_field['value'],
    '#format' => $my_richtext_field['format'],
  );

Comments

snyderp’s picture

For anyone looking for how to set a field to be wyiswyg / #type = 'text_format' using the Field API, here is an example:

$a_field = array(
    'example_text_field_name' => array(
      'field_name' => 'example_text_field_name',
      'default_value' => array(array('value' => '<p>' . $t('Default HTML that will go in the textarea / WYSIWTG editor.') . '</p>')),
      'label' => $t('Example WYSIWYG Text Field'),
      'description' => $t('A description that will be placed under the editor'),
      'widget' => array(
        'type' => 'text_textarea',
      ),
      'settings' => array(
        'text_processing' => '1',
      ),
      'display' => array(
        'success_text' => array(
          'label' => 'above',
          'type' => 'text_default',
        ),
      ),
    ),
  );

Sorry if that was obvious to anyone else. I just spent a long while trying to figure out why my 'settings' values weren't being passed through to the 'text_textarea' widget until I stepped through things and realized that the 'text_processing' key needs to go in a 'settings keyed array in the root of the field definition array, not nested in the 'widget' settings.

Hope thats helpful to someone!

scott.whittaker’s picture

This leads to an error: Fatal error: Unsupported operand types in /includes/form.inc on line 1706 for me (D7, wysiwyg module, ckeditor)

davidn’s picture

At least for me, using WYSIWYG with the field API works if and only if you use

'settings' => array(
  'text_processing' => '1',
),

within an instance (i.e. field_create_instance()).

Of course you can also use 'instance_settings' on the field type definition but if you are using fields of type text or text_long than text_processing is set to 0 by default most likely.

areynolds’s picture

You're able to define the '#format' by the string id of the filter you desire as well, although this is obviously not as flexible as other methods. I was having difficulties with filter permissions and text_format elements and using the id helped me out.

ex:

  $form['body'] = array(
    '#type' => 'text_format',
    '#title' => 'Answer the Question',
    '#rows' => 5,
    '#resizable' => FALSE,
    '#default_value' => 'Write your answer here.',
    '#format' => 'filtered_html',
  );

--Alec

Tandem

alexbern’s picture

this causes an sql access violation when I view my formated form:

•Warning: Illegal offset type in isset or empty in locale() (Zeile 669 von C:\xampp\htdocs\drupal-dev\modules\locale\locale.module).
•PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'plain_text' AND s.context = '' AND s.textgroup = 'default'' at line 1: SELECT s.lid, t.translation, s.version FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.source = :source_value, :source_format AND s.context = :context AND s.textgroup = 'default'; Array ( [:language] => de [:context] => [:source_value] => sgdfgsdfg [:source_format] => plain_text ) in locale() (Zeile 676 von C:\xampp\htdocs\drupal-dev\modules\locale\locale.module).
scott.whittaker’s picture

None of the examples here seem to work in D7 for a system settings form. I can get the text format selector to display but the WYSIWYG editor never appears.

[Update]: Actually it does work, this seems to be an issue with a multisite install using the same db and code, but just a different theme under a subdomain. Even though both versions are using the same admin theme (Seven), the WYSIWYG shows up on one but not the other.

arnoldbird’s picture

For me, none of the examples work for a system settings form. I haven't been able to find any examples that work. Mine is not a multisite install.

arnoldbird’s picture

Nevermind. The example in the article does indeed work in a system settings form.

dropletz’s picture

is there any way to process the formatted text to be ready for print?

adam1’s picture

I used your info to build a custom theme-settings.php, which enables a new textarea with textformat in my theme-settings.

        function paper_form_system_theme_settings_alter(&$form, &$form_state)  {
            $form['paper_data'] = array(
            '#type' => 'text_format',
            '#title' => 'Put Text in here:',
            '#rows' => 5,
            '#resizable' => FALSE,
            '#default_value' => 'xyz..',
            '#format' => 'full_html'
          );
        }

The form works perfektly, but i am not able to access/print the value in my page.tpl.php. The following code produces only the word "Array", because the value stored in the database is not the content of the variable but the Word "Array":

 $pdata = theme_get_setting('paper_data');
    echo $pdata 

What's wrong here?

dropletz’s picture

you must use check_markup function to process the filter format on your text before printing:

$pdata = theme_get_setting('paper_data');
print check_markup($pdata['value'], $pdata['format']);
jvdurme’s picture

This gets rid of the word "Array", but the value in the text_format field (which is a wysiwyg editor) is not saved. I get this error upon saving:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1572 of /var/www/switch/drupal7/includes/bootstrap.inc).

Wakken!

moshansky’s picture

Try:

<?php
 $pdata = theme_get_setting('paper_data')['value'];
    echo $pdata 
?>
SuperEngineer 64’s picture

How can I fix the problin within the hook_edit_form?
Iam building a custom panel pane, and every thing works find with textfields. But text_format will not bs saved.

function text_edit_form($form, &$form_state) {
  $conf = $form_state['conf'];

  $form['form_text'] = array(
    '#type' => 'text_format',
    '#title' => t('Text'),
    '#format' => 'filtered_html',
    '#size' => 50,
    '#resizeable' => TRUE,
    '#default_value' => ???,
  );

  return $form;
}

regards,
SE64