Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a specific need where if a user is created and assigned to the role "manager" that user should be automatically subscribed to all groups. I can do this without issues if I hard code the uid. I have problems when i use hook_user_insert and I am not sure why. The problem is that none of my code actually executes. The hook does fire when I load a user object of a hard coded uid however.

Here is my hook:

 function mm_custom_user_insert(&$edit, &$account, $category)

    if(in_array('manager', $account->roles)) {

      //get all gids
      $results = db_query("SELECT gid FROM og");
      $gids= array();
      while($record = $results->fetchAssoc())
        $gids[]= $record['gid'];

      foreach($gids as $gid)
        og_group($gid, array(
                "entity type"       => "user",
                "entity"        => $account,
                "membership type"   => OG_MEMBERSHIP_TYPE_DEFAULT,
        og_role_grant($gid, $account->uid, 3);  


My main questions are a.) are the user->roles not available at this time? how would I find this out? b.) could it be that the the uid is not available?

Appreciate the help

share|improve this question
Don't use hook_user_insert to create users... use user_save(), it is much cleaner and you can set everything you want. – Brandon Bearden Jul 9 '12 at 5:38

The user ID is definitely available, as hook_user_insert() is called after the user data has been committed to the database.

I've just put the following code into a test module called foo:

function foo_user_insert(&$edit, $account, $category) {

Then I created an account through the admin UI and the output from dpm() looks like this:


So as you can see, the roles are available too.

If you're not familiar with the dpm() function, it's part of the Devel module. It will print any PHP variable to the standard messages area and will display objects/arrays like the screen shot above. It should help you with most of your debugging needs.

share|improve this answer
Thanks Clive, I reviewed the user module to see where the hook was being called and I found that the user roles are available but only by id as they are stored to the user object after the hook. – user975044 Jun 3 '12 at 22:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.