2012-09-18 125 views
1

我的自定義註冊表單模塊具有允許新用戶選擇一個或多個城市的選擇字段。在我的搜索中,我沒有找到任何可供借鑑的例子。例如,如果要保存的城市關鍵字是20和53,是否有人有下面的代碼應該看起來像'field_city'=>的例子?user_save()具有多個選擇字段,如複選框或選擇字段

$edit = array(
    'name' => 'tester', 
    'pass' => user_password(), 
    'mail' => '[email protected]', 
    'status' => 0, 
    'init' => '[email protected]', 
    'access' => REQUEST_TIME, 
    'field_city' => , 
    'field_zip_postal_code' => array(LANGUAGE_NONE => array(array('value' => '12345'))), 
    'field_gender' => array(LANGUAGE_NONE => array(array('value' => 'Male'))), 
); 

user_save(drupal_anonymous_user(), $edit); 

回答

1

我想通了這一個自己出去。如果它幫助任何人,對於分類術語引用,列名是'tid',而不是'value',因爲它是用於其他字段。我改變了登記代碼爲以下:

$edit = array(
    'name' => $form_state['step_information'][2]['stored_values']['name'], 
    'pass' => user_password(), 
    'mail' => $form_state['step_information'][2]['stored_values']['email'], 
    'status' => 0, 
    'init' => $form_state['step_information'][2]['stored_values']['email'], 
    'access' => REQUEST_TIME, 
    'field_zip_postal_code' => array(LANGUAGE_NONE => array(array('value' => $form_state['step_information'][3]['stored_values']['postal_code']))), 
    'field_gender' => array(LANGUAGE_NONE => array(array('value' => $form_state['step_information'][3]['stored_values']['gender']))), 
); 

foreach(array_keys($form_state['step_information'][1]['stored_values']['cities']) as $key => $val) { 
    $edit['field_city'][LANGUAGE_NONE][$key] = array('tid' => $val); 
} 

$new_account = user_save(drupal_anonymous_user(), $edit); 

在「的foreach」循環,「$鍵」是獲取存儲在「增量」列中的基數(0,1,2 ...)在數據庫中,'$ val'是城市的ID。