Users on my site all have a "home region" field. This profile field is saved for each user and used to customise various site features, so that the user gets shown local content.
These regions aren't states or cities - they're more like electorate / local council boundaries (I'm not really sure what the US analogy would be). Most users are going to be uncertain about which region they actually want to save to their user profile.
I have a form which allows users to either (A) explicitly select their region from a select dropdown or (B) enter their postcode, submit the form, and get back their region. This form works fine.
I want users to be able to pick a region at registration time using this form, and have that region saved to their profile. Ideally I'd like to integrate the "select region" form with the user_register form, so that they can submit the form, get told "you are part of X region" and then click "register" and create a new user account with that region.
What I'm doing is using hook_user() to insert my form elements, perform my extra validation and then try to save the region data to the user on submit.
EDIT: the below is roughly how I solved this
function mymodule_user($op, &$edit, &$account, $category = NULL) {
/* add "select your region" functionality to the user edit form */
$category = 'Regional information';
switch ($op) {
case 'register':
case 'form':
/* "register": The user account registration form is about to be displayed. The module should present the form elements it wishes to inject into the form. */
/* "form": The user account edit form is about to be displayed. The module should present the form elements it wishes to inject into the form. */
$form = array();
$form['Regional information']['region'] = _region_select('', _region_select_options());
$form['Regional information']['postcode'] = _postcode();
return $form;
break;
case 'validate':
/* "validate": The user account is about to be modified. The module should validate its custom additions to the user object, registering errors as necessary. */
$region = $account['region'];
$postcode = $account['postcode'];
// form_set_error() if I need to
break;
case 'insert':
case 'update':
/* submit happens *before* the user account is saved - we want insert or update
"insert": The user account is being added.
"update": The user account is being changed.
The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit. */
$region_code = $edit['region'];
$postcode = $edit['postcode'];
if (!$region_code && $postcode) { // pull the region from the postcode
...
}
if ($region_code) {
$region_term = taxonomy_get_term($region_code);
$region_name = $region_term->name;
/* the profile module wants the name of the region, not the id */
$profile_edit = array( 'profile_region' => $region_name );
profile_save_profile($profile_edit, $account, $category);
}
break;
}
}