3

I thought that I needed to, in my submit handler function, call user_save() to save my user profile data. But when I call user_save I get duplicate entry errors because it appears user_save is being called twice.

here's a snippet of code:

function all_data_form_submit($form, &$form_state) {


  $fname = $form_state['values']['field_first_name']['und'][0]['value'];
  $lname = $form_state['values']['field_last_name']['und'][0]['value'];


$edit = array(
  'name' => $form_state['values']['name'],
  'pass' => user_password(),
  'mail' => $form_state['values']['mail'],
  'init' => $form_state['values']['init'],
  'status' => 0,
  'access' => REQUEST_TIME,
    'field_first_name' => array(LANGUAGE_NONE => array(array('value' =>
$form_state['values']['field_first_name']))),

);


user_save(drupal_anonymous_user(), $edit);
<snipped>
  • I'm not sure why you want to programmatically do that when you can manage custom fields through the UI. Or maybe you need to install the Entity API module for that. I forgot if that functionality is in core or by the Entity API. – Pax Sep 13 '13 at 22:05
  • I have a customized user registration page, that has various profile fields that the user can populate. It's that data I can't seem to get saved to the database. Entity API is installed. – StevenPatz Sep 16 '13 at 14:59
2

If you insist use code I suggest below method

function all_data_form_submit($form, &$form_state) {
$fname = $form_state['values']['field_first_name']['und'][0]['value'];
$lname = $form_state['values']['field_last_name']['und'][0]['value'];
$name=trim($form_state['values']['name']);
$myuser=user_load_by_name($name);
if(!empty($myuser)){
        $edit = array(
                'field_first_name' => array(LANGUAGE_NONE => array(array('value' =>$form_state['values']['field_first_name']))),
                // .. and every field you want to save
                );
        user_save($myuser, $edit);
    }
}
1

Not enough points to comment yet, but make sure the Field UI module is enabled, then in /admin/config/people/accounts/fields you can add/remove as you see fit. When adding fields, there is an option to 'Display on user registration form.' field on account

  • Yep, Entity UI is installed and Display on user registration form is check. The issue I am having is when the user is on that form and he enters all the data, none of the profile fields are being saved to the database. – StevenPatz Sep 16 '13 at 15:00
  • @Steven That's strange. Are you sure you looked at the table field_data_{field_machine_name} and filter the entity_type to 'user'? Also, since you know about the Entity UI, why are you trying to save it programmatically? – Pax Sep 16 '13 at 16:19
  • I am customizing the User registration page, so that in addition to username and email I am collecting First Name, Last Name, etc. These are all profile fields I've created. When fill out the form, the normal items are saved ( username etc ) but the field_* items are not being saved. That's what I am trying to accomplish. – StevenPatz Sep 16 '13 at 19:30
  • What is the outcome if you remove/disable the code you shared above? – twardnw Sep 17 '13 at 19:58
  • If I remove that code, the user gets created but none of the profile field info is saved. I can't quite grasp, reading forum postings and the Drupal API pages what I am missing here. The user gets created. None of the profile fields are added to the database. – StevenPatz Sep 17 '13 at 20:42
1

It turns out that if you set your profile to be hidden in your module, you can't save any data to that profile.

0

Save the new users with rudimentary data and then use Entity API.

I am sure this entity module exposes API to load and attach the other fieldable data and save user entities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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