Custom field widget with text field formatter? I'm trying to create a compound field in Drupal 7 that combines and image field, text field and a textarea field.

I've successfully done this but I'm now trying to add text format option to the textarea and I'm having problems getting it working.

It seems that when I change the widget's textarea field into a text_format field Drupal expands that field into two fields value and format (see: filter_process_format), this means that when the node displaying the field is saved it stores the array into the database and not the field value.

Here is my widget:

/**
  * Implements hook_field_widget_form().
  */
function MYMODULE_field_widget_form(&amp;$form, &amp;$form_state, $field, $instance, $langcode, $items, $delta, $element) {

    $elements = image_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);

    foreach (element_children($elements) as $delta) {   
        foreach ($elements[$delta]['#process'] as $key =&gt; $process) {
            if($process == 'image_field_widget_process') {
                unset($elements[$delta]['#process'][$key]);
            }
        }   
      $elements[$delta]['#process'][] = 'MYMODULE_widget_process';  
    }

    return $elements;
}

/**
 * Element #process callback function.
 *
 * @param array $element
 * @param arrayreference $form_state
 * @param array $form
 * @return array
 */
function MYMODULE_widget_process($element, &amp;$form_state, $form) {

    $element = @image_field_widget_process($element, $form_state, $form);

    $item = $element['#value'];
    $item['fid'] = $element['fid']['#value'];

  $instance = field_widget_instance($element, $form_state);

    $settings = $instance['settings'];
    $widget_settings = $instance['widget']['settings'];  

    $element['title'] = array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; 'Title',
        '#default_value' =&gt; isset($item['title']) ? $item['title'] : '',
    );

    $element['text'] = array(
        '#type' =&gt; 'textarea',
        '#title' =&gt; 'Text',
        '#default_value' =&gt; isset($item['text']) ? $item['text'] : '',
    );  

    if($instance['settings']['text_processing']) {
      $element['text']['#type'] = 'text_format';
      $element['text']['#format'] = isset($item['format']) ? $item['format'] : NULL;
      $element['text']['#base_type'] = 'textarea';
    }

    return $element;
}


The field value in the database is "Array" and not the string the user entered. If the field is set to a normal textarea the field value is stored correctly.

I'm guessing this is something to do with the structure of the widget but I can't work it out and the documentation is non existent.
I recently needed to create a widget for file field, that among other things needed to be able to handle multiple uploads via plupload.

Anyways, during the process of getting everything to work, I discovered the #value_callback attribute that you can put on a form item. It allows you to change the data of the submitted data and extra extra infomation, like fid of a file.

This isn't documented yet, but you can take a look at it's use in the file module. There is an issue for this on the Drupal issue tracker, but no good patch has been created with some document example yet.

I'm not sure if this would be a good case for the use of #value_callback, but it seems to me, that it could be of good use in this case.