function user_example_user

Implementation of hook_user().

There is often confusion between the $edit and the $account parameters. $edit is an array, and consists only of the subset of data that is being changed for the user account. When saving a new account, this will contain quite a bit of information (user name, email address, etc) whereas updating an existing account will contain just the fields being modified. $account is a user object containing the account details as they existed when it was loaded with user_load. If your code needs to access a common user property such as uid, nearly always it should come from $account->uid instead of the $edit array.

Related topics

File

user_example/user_example.module, line 55

Code

function user_example_user($op, &$edit, &$account, $category = NULL) {
    // In general, it's good practice to refactor anything longer than a few
    // lines into their own subfunctions. Then it becomes possible to split them
    // out into separate files using module_load_include() to load the include
    // file before calling the function.
    switch ($op) {
        case 'delete':
            db_query("DELETE FROM {user_example} WHERE uid = %d", $account->uid);
            break;
        case 'form':
            // This ensures that our form elements are only added on the "Edit" tab,
            // and not any other subtasks on the form.
            if ($category == 'account') {
                return _user_example_add_color_element(&$edit, &$account);
            }
            break;
        case 'insert':
        case 'update':
            // This function handles both the update and save cases, since it's
            // possible to update a user account that doesn't have a row in the
            // {user_example} table yet.
            _user_example_color_save(&$edit, &$account);
            break;
        case 'load':
            if ($favorite_color = db_result(db_query("SELECT favorite_color FROM {user_example} WHERE uid = %d", $account->uid))) {
                $account->favorite_color = $favorite_color;
            }
            break;
        case 'validate':
            _user_example_color_validate(&$edit, &$account);
            break;
    }
}