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

hargobind’s picture

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/).

Fuuuuuck’s picture

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

wheelercreek’s picture

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:

function mymodule_user($op, &$edit, &$account)
{
   switch($op)
   {
     case 'register':
        $form['allow_img'] = array(
          '#type' => 'checkbox',
          '#title' => t('Please include my picture in Employee Directory search'),
          '#prefix' => '<h3 class="legend">Employee Search Preference</h3>',
         ); 
         return $form;
         break;
      case 'form':
         //load up the user in question
         $usr = user_load(array('uid' => $account->uid));
          // see if picture is listed
         $data = unserialize($usr->data); 
         $attr = (!empty($data['allow_img'])) ? array('checked'=>'checked'):'array()';
         $form['allow_img'] = array(
             '#type' => 'checkbox',
             '#title' => t('Please include my picture in Employee Directory search'),
             '#prefix' => '<h3 class="legend">Employee Search Preference</h3>',
             '#attributes' => $attr,
          ); 
          return $form;
          break;
     }
}

That's it, inserts & updates happen automatically.

Fuuuuuck’s picture

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.

wheelercreek’s picture

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.