Hi,

I'm looking to insert some session data into a seperate table when a user registers.

I've looked at hook_user and sort of understand how it works but can't figure out where to call this function from.

What is the best way to accomplish this task?

Thanks

Josh

Comments

rszrama’s picture

Hey Josh,

The way hooks work, Drupal calls those functions automatically. So, you'll have to create a custom module that has the following function in it:

function module_name_user($op, $edit, $account) {
  switch ($op) {
    case 'insert':
      // Do your code here.
      break;
  }
}

Once that module is enabled, assuming you named the function correctly, your code will get executed every time a new user completes registration at your site.

(Don't forget your .info file!)

----------------------
Current Drupal project: http://www.ubercart.org

joshmcadams’s picture

function module_name_user($op, $edit, $account)

Is "module_name" being the actual name of the module I create?

So if I called the module productSamples then the function would look like:

function productSamples_user($op, $edit, $account)

How does this access hook_user?

Does drupal look for any function with "_user" after it and then run the hook?

Thanks for the input. It was very useful and insightful.

Josh

rszrama’s picture

You're exactly right. The function name is correct, and your understanding of hooks is correct.

Drupal keeps a list of all the modules you have enabled each time it serves a page. There is a function (module_invoke_all())that will append the hook name (in this case '_user') to every module name in the list and use PHP's function_exist to see if a hook function has been written for that hook in that module. If it exists, it gets fired off with whatever arguments apply at that time. : ) It's a brilliant system that makes developing modules very coder friendly.

----------------------
Current Drupal project: http://www.ubercart.org

JStarcher’s picture

If there are two of the same hooks can you over which is last so you can in affect override some settings in the module that goes before it?

rszrama’s picture

Hmm... this may work in some of the hooks, but it's certainly not advised. For most of the hooks this just either won't work or will cause problems. I certainly wouldn't rely on the method.

----------------------
Current Drupal project: http://www.ubercart.org

JStarcher’s picture

Actually I found out that you can adjust the module weight to allow a certain module to hook before or after :)

FrontBurner’s picture

At this point the user hasn't actually been created...there is no user id. If I wanted to add some data to a table using the user's id at the point of registration how could this be accomplished without using the "login" action or some other common action? It would be nice if there were a "registered" action that is called right after the user has been inserted and a user id has been created.

Dave Hirschman’s picture

In Drupal 6.x, the user has already been created when hook_user('insert' , ...) is called, and the uid is available. See user.module around line 338 (in Drupal 6.10).