[development] Creating a user and assigning a node in the node's submit callback.

Brian Vuyk brian at brianvuyk.com
Tue Jan 12 21:24:00 UTC 2010


Hi all.

I've been trying to get a bit of code working, and I would appreciate a 
set of eyes.

Basically, I have a content type ('profile') which has fields for user 
account details. If they are filled out, and a 'Create account' checkbox 
is checked, a user account is created with those details, and the 
profile node should be assigned to the newly-created user instead of the 
user creating the node.

The issue is that, while the new user is created properly in the submit 
hook, the last line in the submit callback where I override the 'uid' 
value from the form values doesn't work - the node just saves with the 
user's uid instead of the uid of the new account that is specified on 
the last line.

Does anyone have any idea how to make this work?

Thank you in advance for anyone that can suggest a solution!

Brian

<?php
function mymodule_form_profile_node_form_alter(&$form, &$form_state) {
   // Add our submit handler. We use array_unshift to prepend it, as we 
want it
   // to execute before the regular node form submit handler.
   array_unshift($form['buttons']['submit']['#submit'], 
'mymodule_profile_form_submit');
}

// Submit the user account creation fields in the profile_node_form.
function mymodule_profile_form_submit($form, &$form_state) {

   // Create the user account.
   if ($form_state['values']['create']) {
     $account['name'] = $form_state['values']['username'];
     $account['mail'] = $form_state['values']['mail'];
     $account['pass'] = $form_state['values']['pass'];
     $account['status'] = 1;
     $account = user_save(array(), $account);

     // Now set the uid of the profile node to be owned by this new account.
     $form_state['values']['uid'] = $account->uid;
   }
}
?>


More information about the development mailing list