By Fuuuuuck on
I'm trying to create my first user-based module and have some problems with understanding hook_user. Could you give very simple example of using it? For example create new textfield on user acount editing page... with demonstrating common actions like save, update, delete and validate this textfield... thanks.
Comments
Look at other modules
Try looking at other modules that are using hook_user(). Open up your Drupal codebase and search all files in the /modules/ folder for "_user(" to see how some of the core Drupal modules are using it [at least half of the modules are using it]. Or search your contributed modules folder (typically /sites/all/modules/) for the same thing. If you're on Windows and need a good file searching tool, I recommend Agent Ransack (http://www.mythicsoft.com/agentransack/).
Thanks
Before posting this topic i've seen the source of profile module, it was a bit diffucult to understand, but after one hour of cutting lines i've understand how to use hook_user...
I think hook_user needs some aditional examples in api.drupal
example
Hopefully I can save you tons of time on this one - it's very simple (but the documentation does NOT make this clear). If you just want to add extra fields, you don't need case insert or case update - in fact they are a bit dangerous. Simply using case "register" and case "form" is all I needed. (Drupal 6).
The results will automatically be stored into the user table, serialized into the data column. The reason I say insert & update are a bit dangerous, the hook is called when a user logs out. In my case, I attempted to save the custom fields I made within case "update" - which worked fine, but then on user logout, the field value wasn't found and so it was obliterated.
here is my sample:
That's it, inserts & updates happen automatically.
How to get this information for listings?
As i understand it stores serialized array in database... so how can i get information for listings? Before i was using INNER JOIN to collect information.
getting information back out
sorry this is such a late response, but for anyone else - you can use the php unserialize function to read the data back out of the database. So, if you look at my example - what I've done is to add a checkbox to the user login fields. In in the case 'form', I'm reading the custom fields with unserialize.