2

I am trying to build a custom field in drupal 7. evrything work just fine, except from the wysiwyg field. I am using the next code to build an WYSIWYG element:

$element['my_body'] = array(
  '#title' => t('Editor'),
  '#type' => 'text_format',
  '#tree' => true,
  '#rows' => 20,
  '#format' => 'filtered_html',
 );

When its not wysiwyg (regular textarea) all save go fine, but after i change it to text_format, drupal get the value of the field as array with 2 keys (value and format), and that's make an error while drupal save the values of the field. As much as i understnad it, what drupal expect to get is two deferent values (of body_filter and format) and not an body_filter array with 2 keys (value and format).

Anyone can give me a hint how to solve this issue (can't find anything relevant in google and drupal.org)?

Thanks.

Barak Bloch
  • 21
  • 1
  • 2

3 Answers3

5

I bumped into the same problem, and found a solution, thanks to Berdir's hint.

As you mentioned 'text_format' returns an array with two values like:

$items[0]['MY_WYSIWYG_FIELD'] = array(
    'value' => 'some text.',
    'format' => 'filtered_html'
);

Using hook_field_presave() I was able to prepare the values to save it into my db.

As I don't want to save the format value, I simply extract the text value from the ['MY_WYSIWYG_FIELD'] array and replace the array with the extracted value:

$items[0]['MY_WYSIWYG_FIELD'] = 'some text.';

my hook looks like this:

function MY_FIELD_MODULE_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
    if ($field['type'] == 'MY_FIELD_TYPE') {
    foreach ($items as $delta => $item) {
    if (isset($item['MY_WYSIWYG_FIELD'])) {
        $value = $item['MY_WYSIWYG_FIELD']['value'];
        $items[$delta]['MY_WYSIWYG_FIELD'] = $value;
        }
        }
    }
}

Hope this helps!

Stefan Yanku
  • 91
  • 1
  • 6
1

The value of text_format type fields come as $form_state['values']['my_body']['value'].

Perception
  • 79,279
  • 19
  • 185
  • 195
modhura
  • 11
  • 1
0

Yes, the value of a text_format field is an array, that's how it is supposed to be.

Drupal does not save something automatically, where and how are you saving it? You simply need to fix that code to work with an array.

Berdir
  • 6,881
  • 2
  • 26
  • 38
  • As much as i understand it, Drupal is actually save the data automatically. Drupal do the "magic" by checking the db field names (as created in the module.install file) against the fields name in the form. This is happening in field_sql_storage_field_storage_write() – Barak Bloch Jun 05 '11 at 15:15
  • 1
    Ah, you are creating a field. Yes, then these values are automatically saved according to the schema. *However*, you can change it by implementing http://api.drupal.org/api/drupal/modules--field--field.api.php/function/hook_field_presave/7 and split the array in two separate values, according to your schema. Also, you could look at how text.module does it, which has the same "problem", I wasn't able to figure that out, however. – Berdir Jun 05 '11 at 17:58