By newbstah on
Hi There -
When a user registers, I'd like to create a few nodes that will be owned or authored by the user. The issue I'm having is that I don't know whether $user->uid is valid for $op insert, and insert looks like it's the function I want.
Here's the documentation for hook_user: http://api.drupal.org/api/function/hook_user/5
Here are the other possibly relevent defs:
"insert": The user account is being added. The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit.
"after_update": The user object has been updated and changed. Use this if (probably along with 'insert') if you want to reuse some information from the user object.
"register": The user account registration form is about to be displayed. The module should present the form elements it wishes to inject into the form.
&$account The user object on which the operation is being performed.I think what I want is after_update only if the insert operation has taken place. I don't want the user to receive a new node each time they change something. I have no idea how to do that since the ops are on a switch statement.
Any advice?
Comments
Use 'insert', $user->uid
Use 'insert', $user->uid should be set and valid.
nevets! Once again, thank you.
So many times when you've helped, the info you share isn't in the documentation. How do you know where to find this stuff out? How do you _learn_ to understand Drupal with such depth? When I searched for hook_user there wasn't any info on whether the uid would be set on insert. I had no idea where to go next.
Please write a book! Your combination of generosity and patient insight would help so many people understand Drupal in a way that no one else could. You could teach people how to learn Drupal faster and with greater depth and elegance. (Like you do now with the forums, but it would all be in one place, and you could sequence the lessons/chapters together in ways that would allow us to see the whole elephant, not just the part we've blindly got our hand on.)
You could use so much of the material that you've already created! Because you have spent so much time in the forums, you probably have a fantastic understanding of where people are tripped up when they're learning. Your natural teaching ability would be informed by all your experience here.
It would also give us a chance to give back to you! Half the time when I'm searching the forums for an answer, you're the one who answers it. In the mean time, how about linking to your site from your profile and doing another fundraiser? Amazon wishlist?
Thank you so much again. :-)
Thanks for the kind words.
Thanks for the kind words. I am a tinker at heart and have learned a lot about trouble simply by writing odd modules just for the fun of it.
Hmmm. . . not sure what I'm doing wrong here.
Hi Again. Not having as much luck as I'd hoped. I have the code that goes inside the case 'insert' working fine in a php enabled page, but it's just not creating the nodes when someone registers when I have it inside the hook_user / 'insert' framework. Here's what I'm doing:
Any ideas why this wouldn't fire on user registration? Haven't been able to find many examples, but some of the ones I've seen don't have a 'break;' at the end. When would you not use break? (BTW, it doesn't work whether I have a break in there or not.)
Also, right now, I've got it in a module where I'm throwing my miscellaneous functions. Have you found that it's a good idea to have a separate module for hook_user that you can keep adding to as the site evolves?
Thanks!
dang! Creates nodes for anonymous!
Will try the bio module's trick below. If anyone knows what I need to do to get the 'insert' working for the nid that's being created, please let me know. Are my variables right?
Thing is, I know it can work. In my wanderings I saw an Übercart module that creates expirable coupons when someone signs in and sends them as an email.
http://www.ubercart.org/forum/development/1697/send_out_gift_certificate...
Unfortunately, it doesn't include the 'insert' code. Just the code to create the email coupon.
Any ideas?
That global $user; is the
That
global $user;is the likely source of your problem.Bio Module uses a different function...
Checked out the bio module, which creates a bio node for each user when they register. Would this work for me? Here's what it says:
How does it get the uid when the name hasn't been submitted yet? Does Drupal reserve a uid for each user when the form has been submitted?
Subscribe
Trying to figure out the same thing, global user gives me myself, including $user in the function parameters seems to give me an empty array where as what I need is the UID of the new user :-/
Roger Heathcote - www.technicalbloke.com
Aha...
Had to include _all_ the function parameters below...
Roger Heathcote - www.technicalbloke.com
Subscribing
Is replying to a comment the only way to bookmark something to come back to?
_
If you mean subscribing right in the website-- yes. Though I'll never understand what benefit that has over a nicely organized bookmark folder right in the browser.
however, things with drupal move at lightning speed-- any 3 year old post is not likely to be relevant and certainly not something to bookmark, lol.