I'm working on a custom field widget for Drupal 7. Everything appears to be working correct, but now I'm getting this error:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1565 includes\bootstrap.inc)

My .module hooks:

 * 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['text_long'] = array(
        '#type' => 'textarea',
        '#attributes' => array('id' => 'MYFIELD', 'rows' => '5', 'cols' => '3'),
        '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
  return array('value' => $element);

My guess is that it's something in the way Drupal is attempting to validate my value with check_plain(), but I can't figure out what's wrong.


According to the hook description, $items is an "Array of default values for this field." You are trying to look in an array of arrays in your default value.

You need to change this line:

    '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',

to look in the $items array, rather than the $items[$delta] array (which doesn't exist) by removing ['value'], like this:

    '#default_value' => isset($items[$delta]) ? $items[$delta] : '',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.