I'm creating a module that needs to know the uid of a new user to update a db table. I've tried placing my SQL call in the 'validate' and 'insert' sections of hook_user, but neither work: in validate I get a uid of 0, in insert I don't get a uid at all.

Any help on this? The life-cycle documentation for many of the hooks with operation arguments is pretty poorly documented. I'm not even sure that $user or $edit is valid in a hook_user 'insert'.

Thanks

Comments

heine’s picture

In user.module 'insert' is executed after getting a new UID:

<?
user_module_invoke('insert', $array, $user, $category);
?>

your function hook_user ($type is 'insert'):

function hook_user($type, $array, $user, $category){
  switch($type){
    case 'insert':
      // $user->uid is available here.
      break;
  
 }
}

I did a print_r($user) and I see a UID there.

--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

heine’s picture

Here is a print out of the arguments (nonsense values)

$type: insert
array: Array ( 
    [name] => Heined 
    [mail] => adfasdfasdf@loclhst 
    [submit] => Create new account 
    [form_id] => user_register 
    [pass] => LPDzdUaSU3 
    [init] => adfasdfasdf@loclhst 
    [status] => 1 
    [created] => 1138742407 
    [uid] => 8 
) 

$user: stdClass Object ( 
    [uid] => 8 
    [name] => Heined 
    [pass] => 36d813550ac3f11a22ff1a614e8d5089 
    [mail] => adfasdfasdf@loclhst 
    [mode] => 0 
    [sort] => 0 
    [threshold] => 0 
    [theme] => 
    [signature] => 
    [created] => 1138742407 
    [access] => 0 
    [login] => 0 
    [status] => 1 
    [timezone] => 
    [language] => 
    [picture] => 
    [init] => adfasdfasdf@loclhst
    [data] => 
    [roles] => Array ( [2] => authenticated user ) 
) 
$category: account

--

Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

spankalee’s picture

Thanks a lot Heine,

I'm not sure what I was doing wrong. I'm still a PHP newbie, since I've only started learning it to use Drupal.

The hooks that use operation parameters could still use better documentation.

heine’s picture

I agree about the operation parameters; I usually look for them in core modules, then experiment a little with a test module.
--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

spankalee’s picture

how is print_r working for you?

I'm using it inside hook_user and not seeing any output.

Of course the form sends to /user/register and then the browser is redirected to / so I'm sure that I would see the output. So how do I use print_r (or print for that matter) inside this hook?

Something like this isn't working.

<?php
function permission_user($type, &$edit, &$newuser, $category = NULL) {

	global $user;

	print_r($newuser);
}
?>

Note that I'm adapting the captcha module, which uses newuser as the parameter name, and also has the global $user. I pasted that incase it matters.

Thanks again

heine’s picture

Captcha doesn't care about inserting the user, hook_user is called however.

I've modified captcha_user to print some test information:

/** 
* Implementation of hook_menu() [sic!], for adding form elements & validation.
*/
function captcha_user($type, &$edit, &$newuser, $category = NULL) {

  global $user;
  if($type == 'insert') {
      print "type: ";
      print_r($type);
      print "array: ";
      print_r($array);
      print "newuser: ";
      print_r($newuser);
      print "category: ";
      print_r($category);
  }

  // [rest of the function: switch($type) won't be executed  on insert]
  // What type of registration action are we taking?
  // make sure this is a registration, and captcha is enabled for registration
  if (_captcha_istrue("captcha_user_register") && !$newuser->uid && !$user->uid) {
    switch ($type) {

Results in (beautified):

type: insert
array:
user: stdClass Object (
  [uid] => 13
  [name] => Heinedadsf
  [pass] => 1a806e0311dafbaa780b1cfa01393cc2
  [mail] => email@local
  [mode] => 0
  [sort] => 0
  [threshold] => 0
  [theme] =>
  [signature] =>
  [created] => 1138828825
  [access] => 0
  [login] => 0
  [status] => 1
  [timezone] =>
  [language] =>
  [picture] =>
  [init] => email@local
  [data] =>
  [roles] => Array ( [2] => authenticated user )
)
category: account

Do not add anything for 'insert' in the switch-case that's already there because the blocks entry IF checks that $user->uid AND $newuser->uid are empty.

I hope you can make something of this :-)

--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

spankalee’s picture

Do not add anything for 'insert' in the switch-case that's already there because the blocks entry IF checks that $user->uid AND $newuser->uid are empty.

Yeah, that's exactly what was happening. I didn't notice, but I took out the uid check and it worked.

I still have a problem seeing the output of any print statements in the 'insert' case. If the registration succeeds then the redirect to / prevents me from seeing anything. Is there a way to turn off that redirect for dev purposes?

heine’s picture

Odd, the redirect shouldn't work, because hook_user already send headers...

You can include a die() in the function; that'll take care of the redirect (and everything else).
--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

nevets’s picture

Normally when using print_r I use one of the following two formats.
First to print from within module code I use

<?php
drupal_set_message(print_r($some_variable, TRUE));
?>

Or if calling it from a theme or theme function I often use

<?php
print print_r($some_variable, TRUE);
?>

The second argument tells print_r to return the string it would normally print giving you control of the printing.

spankalee’s picture

Heine,

You've been a great help, and hopefully you will still read this thread. Everything seems to be working fine now except two related problems.

What I've developed is a "Permission Code" module that asks for a code in order for a user to register. This is only important at registration time, and doesn't matter at all when editing users, or if an admin is adding a user.

When a user edits their profile, there's no permission code field, but my permission_user($op='validate') gets called and is looking for one. Then when an admin adds a user the field does show up, but the admin shouldn't be required to enter a permission code.

The solution I can think of is to know the url the form was on that generated the call to permission_user, but I can't find that information in any of the passed parameters.

Is there a way to find out the path, or another solution you can think of?

heine’s picture

There's an easier solution. Compare for example the variables you get passed during the validate:

function test_user($type, &$edit, &$newuser, $category)

When registring:

type: validate
array:
newuser: Array (
  [name] => usernamehere
  [mail] => user@example.com
  [submit] => Create new account
  [form_id] => user_register
)
category: account

When editing:

type: validate
array:
newuser: stdClass Object (
  [uid] => 2
  [name] => usernamehere
  [pass] => 087cd30cd4313b1cfb3aa6569b75227a
  [mail] => user@example.com
  [mode] => 0
  [sort] => 0
  [threshold] => 0
  [theme] => copyrightmyths
  [signature] => Dit is een signature
  [created] => 1136890240
  [access] => 1139084784
  [login] => 1139084765
  [status] => 1
  [timezone] => 0
  [language] =>
  [picture] =>
  [init] => user@example.com
  [data] => a:2:{s:6:"submit";s:18:"Create new account";s:7:"form_id";s:13:"user_register";}
  [submit] => Create new account
  [form_id] => user_register
  [roles] => Array ( [2] => authenticated user [3] => testers )
)
category: account

(newuser is the variablename from captcha)

Testing for the user id that's passed in the object from user.module can easily distinguish wether registring or editing. This is in fact what the captcha module does with !$newuser->uid.

Another difference lies in the $edit variable, I'll only show the form_id here when in $type = 'validate':

Registring: [form_id] => user_register
Editing:      [form_id] => user_edit

I hope this helps.

Good luck.
--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.