user_save()具有多个选择字段,如复选框或选择字段

时间:2012-09-18 16:42:11

标签: drupal-7 drupal-modules

我的自定义注册表单模块有一个选择字段,允许新用户选择一个或多个城市。在我的搜索中,我没有找到任何可以借鉴的例子。有没有人有'field_city'=>的例子?如果要保存的城市密钥是20和53,应该在下面的代码中看起来像?

$edit = array(
  'name' => 'tester',
  'pass' => user_password(),
  'mail' => 'tester@testing.com',
  'status' => 0,
  'init' => 'tester@testing.com',
  '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 个答案:

答案 0 :(得分: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'循环中,'$ key'是一个基数(0,1,2 ......),它存储在数据库的'delta'列中,'$ val'是城市的ID。