0

I'm using the postal address module to obtain a user's postal address when they register on the site. The registration form is created by a custom module I made.

I'm able to get the form to display the postal address fields correctly (they modify based on your country, etc.), but when I go to process the form in HOOK_submit(), I hit a problem.

The submit function adds the user entity and puts the correct information in for the user. It also creates a new line in the field_data_field_postal_address table in the db as it should. But the address fields are all empty.

I think it is very close to working, but I suspect that something is wrong with the structure of the array I'm passing in to the 'field_postal_address' custom entity field.

function user_registration_form_submit($form, &$form_state){
drupal_set_message("User registration submitted");
dsm($form_state);

$edit = array(
    'name' => $form_state['values']['name'],
    'pass' => $form_state['values']['password'],
    'mail' => $form_state['values']['mail'],
    'init' => $form_state['values']['mail'],
    'field_postal_address' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['address']))),
    'status' => 1,
    'access' => REQUEST_TIME,
);
$newUser = user_save(drupal_anonymous_user(), $edit);
}

So, the question is, why is this function injecting an empty row of data into the field_postal_address field?

2 Answers 2

1

I managed to figure out the problem. Apparently you can't load the new user array with the position of the array where the address fields are located. Instead you have to load the address array into a new variable, and pass THAT into the "field_postal_address" entity field.

So, in my solution, I loaded the address array into the $ary_address array. Then I passed that into the postal address array.

$ary_address = $form_state['values']['address']['address'];

$edit = array(
    'name' => $form_state['values']['name'],
    'field_fname' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_fname']))),
'field_lname' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_lname']))),
    'pass' => $form_state['values']['password'],
    'mail' => $form_state['values']['mail'],
    'init' => $form_state['values']['mail'],
    'field_postal_address' => array(LANGUAGE_NONE => array($ary_address)),
    'status' => 1,
    'access' => REQUEST_TIME,
);
$newUser = user_save(drupal_anonymous_user(), $edit);
0

The first argument to user_save() should be the user object for the newly created account not the anonymous user assuming your submit handler runs after the account has been created.

1
  • nah, in this case there is no user yet. This is a registration form. So the user is being created here.
    – DenverMatt
    Jul 3, 2012 at 15:36

Your Answer

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

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