Community Documentation

Developer info (D7+) - Creating a text field with Wysiwyg enabled

Last updated October 4, 2012. Created by pillarsdotnet on March 10, 2011.
Edited by giorgio79, sun. Log in to edit this page.

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

<?php
 
// 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

Adding a WYSIWYG Field using the Field API

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!

This leads to an error:

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

Use it on instances!

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.

Defining a Format by String

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

East Bay Development -- "Powered by Science"

this causes an sql access

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

None of the examples here

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.

is there any way to process

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

How to render the Variable?

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

<?php
       
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":
<?php
$pdata
= theme_get_setting('paper_data');
    echo
$pdata
?>
What's wrong here?

you must use check_markup

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

<?php
$pdata
= theme_get_setting('paper_data');
print
check_markup($pdata['value'], $pdata['format']);
?>

This gets rid of the word

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!

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x, Drupal 8.x
Audience
Programmers
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here