Drupal的字段的控件不保存数据提交(drupal field widget not saving

2019-09-22 11:24发布

我试图创建一个自定义窗口小部件,但是当我提出,Drupal的似乎不保存任何数据。 当使用hook_field_attach_submit()以显示我已经贴什么样的数据,它被列为空。

奇怪的是,如果我改变#type是一个单一的文本框,而不是一个字段,将仅保存已输入的字符串的第一个字符。

这似乎是一个验证问题,但我不知道如何连接它或调试问题。 我在哪里可以从这里走?

<?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 
}

Answer 1:

hook_field_is_empty是强制性的,必须执行以下类似:

 /**
  * 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);
}


文章来源: drupal field widget not saving submitted data