New User, Longtime Coder: Trying to understand Hooks and creating custom Modules/Blocks
zewtastic - June 12, 2009 - 22:57
I am a longtime PHP coder, but a very new Drupal user.
It seems to be quite robust but I am having difficulty grasping how to get access to some of the functionality.
I have gone through the tutorial about creating a Module that adds a block and have that going fine.
What I am now trying to understand is how to use hooks, to get data from Drupla, such as user_names, user_roles, etc. Information that I would act on to display appropriate information.
I have been reading in the Hook.api section but have not found an example how I might use the hooks in my mytest.module, to say get that user information.
Can anyone help me out here or point me to a good example?

You may not need to implement
You may not need to implement hooks in your module to do this. Perhaps you are looking for the user_load, or user_roles api functions? Follow these links to Drupal API...
http://api.drupal.org/api/function/user_load/6
http://api.drupal.org/api/function/user_roles/6
In learning Drupal, it can be helpful to browse the Drupal API Reference. For each function, you'll find links to examples of how the functions are used in core modules. This is the best code to emulate. After that, you can learn much from the contrib modules (which can be easily browsed through the cvs repository).
Menu, $user, and $node
The three things you need to understand first, IMHO, are the "menu" system (which is so much more than just a menu system), the global $user variable, and $node variables (which can be loaded using node_load). Otherwise, the advice above about emulating the code from the existing modules that's available in the Drupal API is excellent advice.
Hooks are handlers
You only use a hook in your module if you want to register a handler or respond to an event of some kind. Hooks are the points where your module can inject code into Drupal.
Just loading user information won't use a hook - you just use the API functions. Of course your module will probably still use hooks to insert that code into Drupal somewhere.
eg creating a block that shows user info will still require hook_block() (you are registering a handler for a new block), but you won't need any of the user related hooks unless you want to respond to user related events (eg adding or deleting a user).
--
Anton