By pyotr777 on
I'm a beginner and don't quite understand how hooks work.
I wrote a module "userdirectory.module" and I want it to perform some actions when new user accounts created, users login, logout etc. So I'm trying to get use of hook_user. Inside the module I wrote a function to catch "user" hook:
function userdirectory_user($op, &$edit, &$account, $category = NULL) {
if ($op == "insert"){
make_log("insert");
} else if ($op == "after_update"){
make_log("after_update");
} else if ($op == "login"){
make_log("login");
} else if ($op == "logout"){
make_log("logout");
}else if ($op == "update"){
make_log("update");
} else if ($op == "delete"){
make_log("delete");
} else make_log("Other operation: ".$op);
return;
}
The problem is that the function is never called whatever users do. I even tried to rename the function to "hook_user" with no effect. What am I doing wrong?
Comments
your code looks right to me
i might be missing something but your code looks good.
no need to rename to hook_user.
i assume your module is properly enabled in the admin/modules interface, and that other of your functions is executing properly.
after you add a new hook, sometimes you need to go to admin/modules and submit the form, so that Drupal resets its cache of which modules are there and which hools are implemented.
dado
Thanks a lot!
It works now.
Works fine
I took the above code, placed in a file called userdirectory.module, add a version of make_log that looks like
Then I enabled the code and visited 'my account', saw the expected messages, did and edit and a submit still seeing the expected messages. So I wonder if the problem is with your version of make_log() and if it is working or not. (on a different note module functions should start with the module name to avoid name clashes, so make_log() should really be userdirectory_make_log())
Thanks!
I renamed the fucntions as you recommend. Now it's working fine!
I am very interested in this,
I am interested in this, I’ve only been using drupal for few hours.
It seems you are logging when users login and logout etc so you can use that information in your own modules.
This is precisely the thing I want to do, I would really like to be able to store data on my tables with member id in the record.
So users have their own records.
Also I would like to be able to retrieve their user name too
So if I its okay to copy your code into a “userdirectory.module” put it in my modules folder
Then I don’t understand “add a version of make_log”
How does one do that?
Where do I put the function in what file?
also once this logging what the users does, how do I get that information into one of my modules.
Suppose I had a greeting module which basically said
Hello (visitor name here) your visitor id is (visitor id here)