0

我已经使用 module_form_alter 钩子构建了一个自定义注册表单。我还使用 db_add_field 将所需的新字段添加到数据库中。现在我可以在用户注册/用户配置文件编辑中将值添加到表中,并且这些值也存储在数据库中。但我无法做的是获取存储在数据库中的值在用户配置文件编辑表单中显示。是否有一个挂钩可以将数据库中的值加载到表单加载中?或者还有其他方法吗?

 function customUser_schema_alter(&$schema) {
   // Add field to existing schema.
   $schema['users']['fields']['detail'] = array(
         'type' => 'varchar',
         'length' => 100,
   );

 }

 function customUser_install() {
   $schema = drupal_get_schema('users');
   db_add_field('users', 'detail', $schema['fields']['detail']);
 }

 function customUser_form_alter(&$form, &$form_state, $form_id) {
 // check to see if the form is the user registration or user profile form
 // if not then return and don’t do anything
   if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
     return;
   }
   $form['account']['detail'] = array(
       '#type' => 'textfield',
       '#title' => t('Additional Detail'),
     );
   }
4

1 回答 1

1

正确的答案需要更多细节。我只能假设你做了什么。

  1. 您向 {users} 表添加了字段。您没有更新使 drupal_write_record 不知道新字段的数据库架构,这就是未填充它们的原因。
  2. 您创建了一个包含字段的新表 {my_table}。

在这两种情况下,您都需要hook_user_insert()

/**
 * Implements hook_user_insert().    
 */
function mymodule_user_insert(&$edit, $account, $category) {
  // Here you add the code to update the entry in {users} table,
  // or int your custom table.
  // $edit has the values from the form, $account->uid has the
  // uid of the newly created user.
}

注意:如果我的第一个假设是正确的,那么这不是 drupal 的方法。您应该改为使用第二种方式。即使在这种情况下,也可以使用 hook_schema 在 mymodule.install 中创建表,而不是执行 db_add_field()。

对于 drupal 7,您可以使用配置文件模块(核心)或profile2来实现这一点。

基于该代码尝试在表单alter中更改为this。

$account = $form['#user'];
$form['account']['detail'] = array(
  '#type' => 'textfield',
  '#title' => t('Additional Detail'),
  '#default_value' => $account->detail,
);
于 2014-03-03T13:13:08.233 回答