1
votes

I'm trying to add custom fields to the user registration form. I need to save 2 of them into the Drupal User's database and 1 into my own separate database.

I'm using hook_form_alter to add the fields and then hook_form_submit to save the fields.

How do I submit the first/last name fields into drupal's user table and then save the SSN into my own? I have a function to switch between the 2 databases already but am just not sure how to use hook_submit_form to save some in one database and some in another?

function myModule_form_alter(&$form, &$form_state, $form_id){


if($form_id == 'user_registration_form'){
    $form['f_name'] = array(
        '#type' => 'textfield',
        '#title' => t('First Name'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('fname'))
        );
    $form['l_name'] = array(
        '#type' => 'textfield',
        'title' => t('Last Name'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('lname'))
        );
    $form['ssn'] = array(
        '#type' => 'textfield',
        '#title' => t('Social Security Number')
        '#maxlength' => 11,
        '#required' => TRUE,
        '#attributes' => array('class' => array('ssn'), 'placeholder' => '999-99-9999')
        );

Would it be something like this?

    function myModule_form_submit($form, &$form_state){
    //Array of information to save into drupal user's table
    $edit = array(
              'f_name' => $form_state['values']['f_name'],
              'l_name' => $form_state['values']['l_name'],
              'second_email' => $form_state['values']['second_email'],
        );
    user_save(drupal_anonymous_user(), $edit);

    drupal_set_message(t('The form has been submitted.'));
 }

I'm not sure if I can use drupal's user_save() function and pass it this new information to save or if I have to do something in the .install file to add these new columns and then target them in the form_submit?

1
use db_insert or db_query functions to insert into the new database. you can call them after user_save function in the submit handlerViswanath Polaki
@ViswanathPolaki ahhh that makes it much more simple. I can still call the user_save() function like I am doing to save the additional fields that I'm adding to the drupal user's table correct?Exziled
yes, user_save will save registration fields and rest of fields you can save it manuallyViswanath Polaki
I will give it a shot! Thank you so much!Exziled

1 Answers

0
votes

In what field will you save the custom field data? You should create a custom table for those fields and save data in there. When you load the user, you should also load those details from that table. There is no way you can directly load those custom field data when user object is loaded.