How to modify a new Drupal user programmatically. Hint: I'm using hook_user for everything

There are many things you may want to do to a new user on your Drupal website. You may want to apply a new role, require approval, or even programmatically create node with the new user as the author. So let's get started...

So the trick here is obviously using the $op insert as it is only called once upon user creation.

For a very basic example of using hook_user(), here is how you would create a new node for the new user. Useful for automatically creating a new user's content_profile node. This is pretty much just programmatic node creation inside of hook_user's insert $op.

/* * Implementation of hook_user */ function mymodule_user($op, &$edit, &$account, $category = NULL) { if($op == "insert"){ // created an empty object $node = new stdClass(); $node->uid = $account->uid; $node->type = 'profile'; // the content type's machine name $node->title = $account->name; // title the node after the author $node->body = ''; // empty body field, or populate with example data $node->status = 1; // published // save the new node node_save($node); } }

Applying a new role to the user. Useful for many things. You could potentially have a profile question on user registration and use the given answer to determine the role the user should receive.

/* * Implementation of hook_user */ function mymodule_user($op, &$edit, &$account, $category = NULL) { if($op == "insert"){ // set the role name and query for the role id $role_name = 'test role'; $role_id = db_result(db_query("SELECT id FROM {role} WHERE name = '%s'", $role_name)); // apply the new role $edit['roles'][$role_id] = $role_name; } }

Now, let's give the user a role based on a profile field.

I've created a profile field named profile_role and made it a select list with the options being Normal User and Business Owner. This case is only slightly different from the previous, in that we need to load the account's profile information, and look at the profile_role field to decide what to do.

/* * Implementation of hook_user */ function mymodule_user($op, &$edit, &$account, $category = NULL) { if($op == "insert"){ // add profile data to the user object profile_load_profile($account); // check for the selected role if($account->profile_role == "Business Owner"){ // set the role name and query for the role id $role_name = 'business owner'; $role_id = db_result(db_query("SELECT id FROM {role} WHERE name = '%s'", $role_name)); // apply the new role $edit['roles'][$role_id] = $role_name; } } }

For the final example, we will block new business owners so that they require admin approval. This is something I've found myself doing on multiple sites recently.

Assume we have a website where the user base is divided into two primary roles. The first being the standard authenticated users, and the other users having a business owner role. A business owner has way more permissions than an authenticated user, so we want to require all business owners to be approved, while allowing standard users to skip approval.

/* * Implementation of hook_user * Handle admin approval for the business owner role */ function mymodule_user($op, &$edit, &$account, $category = NULL) { if($op == "insert"){ // add profile data to the user object profile_load_profile($account); // check for the selected role if($account->profile_role == "Business Owner"){ // set the role name and query for the role id $role_name = 'business owner'; $role_id = db_result(db_query("SELECT id FROM {role} WHERE name = '%s'", $role_name)); // apply the new role $edit['roles'][$role_id] = $role_name; // update the database to set user status to zero db_query("UPDATE {users} SET status = 0 WHERE uid = %d", $account->uid); // set the 'changed' data to NULL so it doesn't save over out database modification $edit['status'] = NULL; } } }

There we have it. Programmatically giving a new user a role based on a profile field, and requiring that user to be approved by the admin.