2

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?

1 Answer 1

0

I believe you're looking for hook_user_presave().

This hook is called directly before the user is saved and the $edit arg will contain all of your form values.

As an alernative, you may look at the Profile2 module

9
  • hook_user_presave() looks like it saves the data serialized in the user object. So if I removed $account = user_save(null,$new_user); changed $new_user to $edit, it should then automatically save the extra field entries in their respective Profile fields? I am already using Profile2.
    – StevenPatz
    Commented Sep 20, 2013 at 12:28
  • hook_user_presave would replace hook_form_submit and you wouldn't need to call user_save at all. Commented Sep 20, 2013 at 12:30
  • Thanks for the pointers but I must be missing the big picture here. Nothing I try gets the data from the extra form elements into a profile field or the data column of the user object.
    – StevenPatz
    Commented Sep 20, 2013 at 13:32
  • Have you implemented MODULE_user_presave(&$edit, $account, $category) and drupal_set_message(print_r($edit,true)) inside the hook? Commented Sep 20, 2013 at 13:44
  • Okay that provided output. The second part of my initial question though, is how do I have these: $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'); fields only show up in the form AND have their data saved? If I unset or hide the profile_memberlist $form array data is not saved at all.
    – StevenPatz
    Commented Sep 20, 2013 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.