Custom Drupal 7 Field only saves the first character

江枫思渺然 提交于 2019-12-04 12:48:00

This widget form worked for me:

function youtubefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta,\
 $element) {
  $main_widget = array();
  switch ($instance['widget']['type']) {
  case 'youtubefield_video_widget':
    $main_widget = $element + array(
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
      );
    break;
  }
  $element['value'] = $main_widget;
  return $element;
}

I had the same problem and discoverd hook_field_widget_form() needs to return: "$element['value']"

function field_youtube_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

    $element['value'] = array(
        '#type' => 'textfield', 
        '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
    );

    return $element;
}

Change your widget form to:

function youtubefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { 
  switch ($instance['widget']['type']) {  
      case 'youtubefield_video_widget':  
        $element['youtubefield_video'] = array(
            '#type' => 'textfield',  
            '#title' => $element['#title'],  
            '#description' => $element['#description'], 
            '#default_value' => isset($items[$delta]['youtubefield_video']) ? $items[$delta]['youtubefield_video'] : NULL,  
            '#required' => $element['#required'],  
            '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,  
            '#delta' => $delta,  
        ); 

      break; 
  } 
  return $element;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!