Creating a user and assigning a node in the node's submit callback.
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; } } ?>
Just a shot in the dark but try setting the node's 'name' value on the node as well. On Tue, Jan 12, 2010 at 4:24 PM, Brian Vuyk <brian@brianvuyk.com> wrote:
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; } } ?>
I just did a very similar functionality only used hook_node_api: case 'insert' to create the user and set the author of the node. Works just fine there and you avoid any edge cases where the form is submited but the node isn't 'save-able'. -S On Jan 12, 2010, at 4:24 PM, Brian Vuyk wrote:
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; } } ?>
I thought about doing it that way, and looking back, perhaps I should have... but the fields we are using are added to the form via hook_form_alter, and aren't actually part of the node. Brian On 10-01-12 04:45 PM, Sam Tresler wrote:
I just did a very similar functionality only used hook_node_api: case 'insert' to create the user and set the author of the node. Works just fine there and you avoid any edge cases where the form is submited but the node isn't 'save-able'.
-S
On Jan 12, 2010, at 4:24 PM, Brian Vuyk wrote:
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; } } ?>
Well, If that is the case then can you add a user reference field to the node that you populate in your form alter with the new users uid (i.e. somehow get the new user's id saved with the node)? Then re-assign in a seperate hook_nodeapi to $node->uid on insert. It's a little hack- ish, but if the only actual issue you are having is assigning the author's uid, then that would do it with a few lines of code. -S On Jan 12, 2010, at 5:31 PM, Brian Vuyk wrote:
I thought about doing it that way, and looking back, perhaps I should have... but the fields we are using are added to the form via hook_form_alter, and aren't actually part of the node.
Brian
On 10-01-12 04:45 PM, Sam Tresler wrote:
I just did a very similar functionality only used hook_node_api: case 'insert' to create the user and set the author of the node. Works just fine there and you avoid any edge cases where the form is submited but the node isn't 'save-able'.
-S
On Jan 12, 2010, at 4:24 PM, Brian Vuyk wrote:
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; } } ?>
Oh, and I'm sure you are considering this, but in your code sample, an anonymous user could, I believe, create multiple user entries with the same email and different uid's, or, just overwrite their existing user profile (which may be fine). This is why I had to write a whole collect_user function that checked the user and if they existed attributed an action to that user in a separate table. Hope this helps. -S On Jan 12, 2010, at 5:36 PM, Sam Tresler wrote:
Well, If that is the case then can you add a user reference field to the node that you populate in your form alter with the new users uid (i.e. somehow get the new user's id saved with the node)? Then re- assign in a seperate hook_nodeapi to $node->uid on insert. It's a little hack-ish, but if the only actual issue you are having is assigning the author's uid, then that would do it with a few lines of code.
-S
On Jan 12, 2010, at 5:31 PM, Brian Vuyk wrote:
I thought about doing it that way, and looking back, perhaps I should have... but the fields we are using are added to the form via hook_form_alter, and aren't actually part of the node.
Brian
On 10-01-12 04:45 PM, Sam Tresler wrote:
I just did a very similar functionality only used hook_node_api: case 'insert' to create the user and set the author of the node. Works just fine there and you avoid any edge cases where the form is submited but the node isn't 'save-able'.
-S
On Jan 12, 2010, at 4:24 PM, Brian Vuyk wrote:
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; } } ?>
Specifically (sorry had to remove some names from the code), in this case we have a separate function that creates the user and returns the user object (we need to check if the user already exists and load it if it does in that function) : function module_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { switch($op) { case 'insert': switch($node->type) { case 'ind_pet': /** Populate a user profile **/ $purpose_user = array( 'mail' => $node->field_mail[0]['email'], 'first' => $node->field_first[0]['value'], 'last' => $node->field_last[0]['value'], ); /** Create the profile if it doesn't already exist **/ $profile = module_collect_user($purpose_user); /** Set the Author properly before saving the node **/ $node->uid = $profile->uid; /** Fire Email **/ actions_do('lsa_ded_book_creation_thank_you_email_action', $node); break; } On Jan 12, 2010, at 4:24 PM, Brian Vuyk wrote:
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; } } ?>
Just to follow up on the original post... The reason this wouldn't save is because of this little code block in node_submit() which is called by the node_form_submit() submit handler, which executes after my custom submit handler: if ($account = user_load(array('name' => $node->name))) { $node->uid = $account->uid; } Setting $form_state['values']['name'] to the new account's name caused the right uid to be assigned. Thanks for the responses and suggestions, all of you! Brian On 10-01-12 04:24 PM, Brian Vuyk wrote:
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; } } ?>
FYI. This is a quirk (?) in node_submit() when a user can 'administer nodes'. Typically, this happens when your form is being run by user 1 or another super user. See: http://api.drupal.org/api/function/node_submit/6 - Ken On Wed, Jan 13, 2010 at 10:46 AM, Brian Vuyk <brian@brianvuyk.com> wrote:
Just to follow up on the original post...
The reason this wouldn't save is because of this little code block in node_submit() which is called by the node_form_submit() submit handler, which executes after my custom submit handler:
if ($account = user_load(array('name' => $node->name))) { $node->uid = $account->uid; }
Setting $form_state['values']['name'] to the new account's name caused the right uid to be assigned.
Thanks for the responses and suggestions, all of you!
Brian
On 10-01-12 04:24 PM, Brian Vuyk wrote:
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; } } ?>
-- Ken Rickard agentrickard@gmail.com http://ken.therickards.com
Speaking of that fun little bit of code, http://drupal.org/node/683630 is a D7 patch I just submitted to remove the hard-coded 'administer users' check and clean up that logic in node_submit(). It could use a review or two. :) Dave Reid dave@davereid.net On Wed, Jan 13, 2010 at 9:46 AM, Brian Vuyk <brian@brianvuyk.com> wrote:
Just to follow up on the original post...
The reason this wouldn't save is because of this little code block in node_submit() which is called by the node_form_submit() submit handler, which executes after my custom submit handler:
if ($account = user_load(array('name' => $node->name))) { $node->uid = $account->uid; }
Setting $form_state['values']['name'] to the new account's name caused the right uid to be assigned.
Thanks for the responses and suggestions, all of you!
Brian
On 10-01-12 04:24 PM, Brian Vuyk wrote:
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; } } ?>
participants (5)
-
andrew morton -
Brian Vuyk -
Dave Reid -
Ken Rickard -
Sam Tresler