I'm creating a custom module that has implemented these hooks:
<?php
/**
* Implements hook_field_widget_info().
*/
function MYMODULE_field_hook_info() {
return array(
'CUSTOM_field' => array(
'label' => t('Custom Text Area'),
'field_types' => array('text_long'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function MYMODULE_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
switch($instance['widget']['type']) {
case 'CUSTOM_field':
$element['MYFIELD_field'] = array(
'#type' => 'textarea',
'#attributes' => array('id' => 'MYFIELD', 'rows' => '5', 'cols' => '3'),
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
break;
}
return array('value' => $element);
}
All it is supposed to do is provide a custom widget to the Long Text field type that enables some custom functionality. My custom widget appears in manage fields and I can enable it and use it on a node edit form. However, when I save the node with text in the field, nothing saves to the database the data saves to the database, but doesn't display in the field when the node is edited again.
After some searching, I found some posts that said hook_field_is_empty() is mandatory and is a common problem for values not saving. I attempted to implement the hook exactly as the API said:
<?php
/**
* Implements hook_field_is_empty().
*/
function MYMODULE_field_is_empty($item, $field) {
if (empty($item['value']) && (string) $item['value'] !== '0') {
return TRUE;
}
return FALSE;
}
This immediately started throwing an error in Drupal:
Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1573 of /includes/bootstrap.inc).
So I looked around some more on here and Google and changed the hook based on this answer:
<?php
/**
* Implements hook_field_is_empty().
*/
function MYMODULE_field_is_empty($item, $field) {
if ($field['type'] == 'text_long') {
if (empty($item[$field['type']]['CUSTOM_field'])) {
return TRUE;
}
}
return FALSE;
}
This returns the same error and it also doesn't save the value of the field into the database display the field data from the database. Do I have to create a custom field with a MYMODULE.install and the appropriate hooks, or can I just use an existing field type like I'm doing. Why is the data not saving to pulling from the database?