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(&$form, &$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 => $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, &$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' => 'textfield',
'#title' => 'Title',
'#default_value' => isset($item['title']) ? $item['title'] : '',
);
$element['text'] = array(
'#type' => 'textarea',
'#title' => 'Text',
'#default_value' => 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.