Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WYSIWYG editor in texarea for Drupal configuration form

Is it possible to use a WYSIWYG editor in texarea for Drupal site configuration form (system_settings_form).

This is how the configuration is coded now...

$form['my_module_text_bottom'] = array(
    '#type' => 'textarea',
    '#title' => t('Some text'),
    '#default_value' => variable_get('my_module_text_bottom', 'This is configurable text found in the module configuration.'),
    '#size' => 1024,
    '#maxlength' => 1024,
    '#description' => t("Some text."),
    '#required' => TRUE,
  );
  return system_settings_form($form);
like image 998
bert Avatar asked Apr 21 '10 15:04

bert


2 Answers

Here it is for Drupal 7 and Drupal 6.

For D7:

<?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'],
  );
?>

For D6:

<?php
  // 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);
?>
like image 64
jacmkno Avatar answered Sep 22 '22 23:09

jacmkno


I kept searching for this issue for about 6 hours and finally i found the reason, for your custom textarea field you must add this line, to use the default input format (Full HTML):

$form['format'] = filter_form();

be careful if you use this form element inside fieldset you must include this fieldset:

$form['donation-instructions']['format'] = filter_form();

I hope this will help you

like image 27
Omnia Avatar answered Sep 24 '22 23:09

Omnia