I'm missing something core to how user registration forms are customized and the data is added to the database and I'm hoping to get a workflow.
I have created about a dozen Profile fields, textareas, radio buttons, select lists, etc.
I have written a module to do a form_alter
, and in that module I have laid out fieldsets and containers so that everything is neat and orderly. For example, for Home Information I have this:
$form['hi'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="hi_cont">',
'#suffix' => '</div>',
'#title' => t('Home Information'),
);
$form['hi']['hic'] = array(
'#type' => 'container',
'#prefix' => '<div id="hic">',
'#suffix' => '</div>',
);
$form['hi']['hic']['left'] = array(
'#type' => 'container',
'#prefix' => '<div id="hic_left">',
'#suffix' => '</div>',
);
$form['hi']['hic']['right'] = array(
'#prefix' => '<div id="hic_right">',
'#suffix' => '</div>',
'#type' => 'container',
);
and then I have this to display Profile fields that are relevent to Home Information:
$form['hi']['hic']['right']['profile_memberlist']['field_home_phone'] = $form['profile_memberlist']['field_home_phone'];
$form['hi']['hic']['right']['profile_memberlist']['field_home_phone']['#maxlength'] = 16;
$form['hi']['hic']['right']['profile_memberlist']['field_home_phone']['#size'] = 13;
$form['hi']['hic']['right']['profile_memberlist']['field_home_phone']['#title'] = t('Phone');
$form['hi']['hic']['right']['profile_memberlist']['field_home_phone']['#description'] = t('Enter your phone number');
$form['hi']['hic']['left']['profile_memberlist']['field_first_name'] = $form['profile_memberlist']['field_first_name'];
$form['hi']['hic']['left']['profile_memberlist']['field_first_name']['#maxlength'] = 16;
$form['hi']['hic']['left']['profile_memberlist']['field_first_name']['#size'] = 13;
$form['hi']['hic']['left']['profile_memberlist']['field_first_name']['#title'] = t('First Name YO');
$form['hi']['hic']['left']['profile_memberlist']['field_first_name']['#description'] = 'HEY THERE';
Now when I view the registration form, I see first all my generic Profile fields, PLUS the elements created from the above. I thought that unsetting the profile_memberlist was the way to go, but it turns out that while the user is created the Profile fields do not get saved this way.
My Submit is something like this:
$form['#submit'][] ='all_data_form_submit';
the actual function:
function all_data_form_submit($form, &$form_state) {
drupal_set_message('<pre>' . print_r($form_state['values'],TRUE) . '</pre>');
$new_user = array(
'name' => $form_state['values']['name'],
'pass' => user_password(),
'mail' => $form_state['values']['mail'],
'init' => $form_state['values']['mail'],
'status' => 0,
'access' => REQUEST_TIME,
'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
'field_last_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_last_name']))),
);
$form_state['redirect'] = array(
'node/31890',
array(
'query' => array(
'foo' => 'bar',
),
'fragment' => 'baz',
),
);
$account = user_save(null,$new_user);
}
But that ends with a DATA INTEGRITY error on submit, because apparently user_save is being called twice? I remove the user_save and the user is created, but none of the Profile fields get added to the database.
What has worked for me is when both the unstyled and the styled form elements are shown, and I don't use a custom submit handler, the Profile data is saved.
My question: how do I display the styled and neatly organized form elements only, and have the data entered through them get saved to the database?