Quick tips on Drupal hook user



So as you see, Im playing a lot with the hook_user function in Drupal. It has a lot of power, but unfortunately,theres not too much documentation to play with itl. I am going to post some random tips that i learn using the hook user function so this will be an article that would be updated frequently. The tips are for the Drupal 6.X version. Lets begin!

About the hook user function
This is an useful hook function when you need to play around with the users in Drupal, particulary registering users, updating users, validating the info of user and the deletion of user. This function receive four parameters:
  • $op: The operation to perform. Refer to the api to know all the variables
  • &$edit: The values returned from the login/register form. This is a reference to the variable, so be carufull changing values here
  • &$account: The user object on which the operation is being performed. This variable is not always the current active user!, so the global $user is not always equals to $account. For example if you are the superuser and you are registering manually a user called Michael, the $account should be Michael and $user should be the superuser
  • $category The active category of user information being edited. This works only for updating user info.

Tip A: The account variable in $op = validate
I have some problems with this. I was trying to access the name of the account like this $account->name, but it gave me an error saying that the object dont have that field.

When debbuging the variable, I could see the field name with the corresponding value.So what was the problem? Well it appears than when validating the form, the $account variable is an stdClass object, and with those object you cant use the -> operator, you can only access their fields using a sintaxis like associative arrays, so the correct sintaxis in this case is $account['name'] instead of $account->name. Have some fun with this

Tip B: Help, i cant debug hook_user using Drupal for Firebug, or drupal_set_message

The thing with hook_user and many hook in Drupal is that they are called over and over again across different pages.

Drupal for Firebug works putting some hidden html to your footer, so if you try to use it , it will not work. I dont know exactly why this happen but, for example, using firep in the validate $op just wont work.

drupal_set_message is an useful function to send messages to your current Drupal user. But I couldnt make it show a print_r($account) or a var_dump($account), for some strange reason , it just dont work.

The best way to debug your code , appart from using xcode, or Zend debugger, or other professional debug tools, is using the firephp extesion of Firefox. Theres a magic option called persist that save every data ouptputed to firephp, until you delete it manually.


The only problem with firephp is that you have to somehow, integrate it with Drupal. Its not so hard and this useful article will help you install it in a generic PHP program. But remember my word, that Persist Button IS your only friend in Drupal debugging, forget devel or Drupal for firebug module, they just dont have the power!

UPDATE 21 September 2010: Actually Drupal for firebug is an awesome module. After writing this article, as some sort of poetic justice, Firephp started to fail, an in exchange, Drupal for Firebug started to work!. So, i would recommend to use both, just in case, first plug-in Drupal for Firebug, and if it fails then launch Firephp and catch your bug.

Also the devel module as some nice functions for development. And lets not forget the dblog module (formally watchdog, you can found him here admin/reports/dblog). If you see some blank pages maybe going here can help you solve your problems.

Nobody says that being an PyroEntomologist was fun.

1 comment:

judy zell said...

We are leading exhibition stand designers and builders based in kuwait. We are involved in designing the exhibition booth and marketing display stalls for running trade fairs, marketing campaigns, promotional events, presentations in various kuwait trade fairs.
exhibition stand contractors