2014-02-28 110 views
0

我已經使用module_form_alter鉤子構建了一個自定義註冊表單。我還用db_add_field將所需的新字段添加到數據庫中。現在我可以在用戶註冊/用戶配置文件編輯中將值添加到表格中,並且值也被存儲在數據庫中。但是我無法做的是獲取存儲在數據庫中的值在用戶配置文件中顯示編輯表單。有沒有一個鉤子來加載數據庫中的值來形成表單加載?或者還有其他方法嗎?Drupal自定義用戶註冊表格

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'), 
    ); 
    } 

回答

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, 
); 
+0

我所做的就是用hook_schema_alter修改用戶表的架構,然後添加兩個字段在模塊的hook_install使用db_add_field用戶表。在用戶個人資料頁面中,我可以看到用戶個人資料編輯/註冊頁面中的其他字段,這些值也在該字段中更新。我想要的和現在不起作用的是當顯示用戶配置文件編輯頁面時,從表格中獲取值的字段顯示在字段中。 –

+0

您是否檢查您的值是否添加到{users}表中?如果他們在那裏。爲這些表單域設置一個「#default_value」=> $ account-> new_field'就足夠了。但是如果這不起作用,我需要查看代碼。 – tic2000

+0

是的,這些值存在於{users}表中。我編輯了我的問題來添加代碼。 $賬戶變量究竟是什麼? –