drupal field widget not saving submitted data

流过昼夜 提交于 2019-12-07 03:26:47

问题


I'm trying to create a custom widget but when I submit, Drupal doesn't seem to save any data. When using hook_field_attach_submit() to display what data I've pasted, it is listed as null.

Strangely, if i change the #type to be a single textfield instead of a fieldset it will save only the first character of the string that has been entered.

This seems like a validation issue, but I'm not sure how to hook into it or to debug the problem. Where can I go from here?

<?php
function guide_field_widget_info(){
  dpm("guide_field_widget_info");
  return array(
    'guide_text_textfield' => array(
      'label' => t('test Text field'), 
      'field types' => array('text'), 
      'settings' => array('size' => 60), 
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    )
  );
}


function guide_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $field_name = $instance['field_name'];
  $required = $element['#required'];
  $item =& $items[$delta];


  $element += array(
        '#type' => 'fieldset',
    '#title' => t('helloooooooo'),
    );
    $required = $element['#required'];
    $item =& $items[$delta];

    $element['nametest'] = array(
        '#title' => t('Name'),
        '#type' => 'textfield',
        '#required' => $required,
        // use #default_value to prepopulate the element
        // with the current saved value
        '#default_value' => isset($item['nametest']) ? $item['nametest'] : '',
    );

    $element['checkme'] = array(
        '#title' => t('Check this box or dont'),
        '#type' => 'checkbox',
        '#default_value' => isset($item['checkme']) ? $item['checkme'] : '',
    );

//When changing the above code to have a single field, $value is no longer  null but will display the first character of the string. I've pasted the code I used to test beloe
/*
  $element+= array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => isset($item['nametest']) ? $item['nametest'] : '',
  );  
*/

  return $element;
}


//hooking this here is required given that after submit, the value is a multidimensional array, whereas the expected value of text is, well, text :-)

function  guide_field_attach_submit($entity_type, $entity, $form, &$form_state){
  dpm($form,"guide_field_attach_submit data"); //shows $form[field_test_field][und][0]    [value] as being null 
}

回答1:


hook_field_is_empty is mandatory and has to be implement like following:

 /**
  * Implements hook_field_is_empty().
  */

function MODULENAME_field_is_empty($item, $field) {
  if ($field['type'] == 'FIELDTYPE') {
    if (empty($item[$field['type']]['YourField']) ) {
      return (TRUE);
    }
  }
  return (FALSE);
}


来源:https://stackoverflow.com/questions/12433039/drupal-field-widget-not-saving-submitted-data

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