Drupal 7 drupal表单字段未加载正确的数据

Drupal 7 drupal表单字段未加载正确的数据,drupal-7,Drupal 7,我正在构建我的第一个Drupal 7模块,在编辑可部署实体的屏幕上遇到了问题。我正在使用field_attach_表单,它对所有接受一个字段都非常有效,该字段显示该实体的字段默认值,而不是该字段的当前内容 我有一个文本字段,一个数字字段,一个布尔字段和一个失败的列表文本字段 你知道我做错了什么吗?下面的代码是我认为需要的,但如果您需要更多,请务必让我知道 在hook_enable中创建字段的代码: if (!field_info_field('field_available')) { $

我正在构建我的第一个Drupal 7模块,在编辑可部署实体的屏幕上遇到了问题。我正在使用field_attach_表单,它对所有接受一个字段都非常有效,该字段显示该实体的字段默认值,而不是该字段的当前内容

我有一个文本字段,一个数字字段,一个布尔字段和一个失败的列表文本字段

你知道我做错了什么吗?下面的代码是我认为需要的,但如果您需要更多,请务必让我知道

在hook_enable中创建字段的代码:

if (!field_info_field('field_available')) {
    $field = array (
      'field_name' => 'field_available',
      'type' => 'list_text',
      'settings' => array(
      'allowed_values' => array('No', 'Provisionally', 'Yes'),
    ),
 );
 field_create_field($field);
 if (!field_info_instance('appointments_status', 'field_available', 'appointments_status')) {
   $instance = array(
     'field_name' => 'field_available',
     'entity_type' => 'appointments_status',
     'bundle' => 'appointments_status',
     'label' => t('Available?'),
     'required' => TRUE,
     'default_value' => array(array('value' => 'No')),
     'description' => t('Set to No if appointments with this status make this slot unavailable, Provisionally means that it will only reserve a space temporarily'),
   );
 field_create_instance($instance);
创建实例的代码,也在hook_enable中:

if (!field_info_field('field_available')) {
    $field = array (
      'field_name' => 'field_available',
      'type' => 'list_text',
      'settings' => array(
      'allowed_values' => array('No', 'Provisionally', 'Yes'),
    ),
 );
 field_create_field($field);
 if (!field_info_instance('appointments_status', 'field_available', 'appointments_status')) {
   $instance = array(
     'field_name' => 'field_available',
     'entity_type' => 'appointments_status',
     'bundle' => 'appointments_status',
     'label' => t('Available?'),
     'required' => TRUE,
     'default_value' => array(array('value' => 'No')),
     'description' => t('Set to No if appointments with this status make this slot unavailable, Provisionally means that it will only reserve a space temporarily'),
   );
 field_create_instance($instance);
此实体只有一个与该实体同名的捆绑包

在hook_菜单中创建URL的代码:

 $items['admin/appointments/appointments_statii/%/edit'] = array(
   'title' => 'Edit appointment status',
   'description' => 'Edit the parameters of the selected status code',
   'page callback' => 'drupal_get_form',
   'page arguments' => array('appointments_status_edit_form',3),
   'access arguments' => array('access administration pages'),
   'type' => MENU_CALLBACK,
 );
表格功能是:

 function appointments_status_edit_form($form, &$form_state) {
   // Get the status id from the form_state args
   $status_id = $form_state['build_info']['args'][0];

   // Load the chosen status entity
   $status = entity_load_single('appointments_status', $status_id);

   // Set up the fields for the form
   field_attach_form('appointments_status', $status, $form, $form_state);

   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => 'Save changes',
     '#weight' => 99,
   );
   return $form;
 }
我使用了Devel模块的dpm来检查entity_load_single是否正确加载了数据,并且数据是否正确

谢谢
罗里

我已经回答了我自己的问题! 我还以编程方式加载了一些实体,并没有加载带有列表文本字段存储的数字的字段,而是加载了可视文本。 我使用了元数据包装器,代码如下所示:

$w_appointments_status->$appointments_availability= 'Yes';
我把它改成:

$w_appointments_status->$appointments_availability = 2;
在本例中,“是”是第三个允许的值-因此为2。 因此,我问题中的代码实际上是正确的,尽管我已经在实例中添加了“widget”和“formatter”参数

我很抱歉,如果这让你们中的一些人挠头想‘但那个代码是正确的’!! 当做 罗里