2

My site is sub-divided into organic groups.

Group administrators are allowed to create new users, but I need a way to auto-assign the new user to the same organic group the creating group administrator belongs to.

Can anyone tell me how I can accomplish this auto-assignment either programmatically or with a module?

4

You should write a custom module that implements hook_form_alter().

  • Get the $form_id of the 'create new user' form that you want to alter.

    ( drupal_set_message($form_id); is an easy way to do this. )

  • Add a custom submit handler to the 'create user' form that uses the respective details and the og_group() function to add the new user to the group.

    function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
    
        if (isset($form_id) && $form_id == 'YOUROG_USERCREATION_FORMID') {
            $form['#submit'][] = 'MODULENAME_submit_handler';
        }
    
    }
    
    
    function MODULENAME_submit_handler($form, &$form_state) {
    
        $gid = $form_state['values']['groupID'];
        $uid = $form_state['values']['userID'];
    
        $account = user_load($uid);
        $values = array(
            'entity_type' => 'user',
            'entity' => $account,
            'state' => OG_STATE_ACTIVE,
           );
    
        og_group('node', $gid, $values);
    
        return $form;
    }
    

Just as a point of information, this code sample is geared towards OG 7.2.x.

If you're using OG 7.1.x, you drop the first argument in og_group, so it would be og_group($gid, $values);

Also, the possible $values you can use, and defaults, are as follows:

# taken directly from og.module

$values += array(
     'entity type' => 'user',
     'entity' => FALSE,
     'state' => OG_STATE_ACTIVE,
     'save' => TRUE,
     'force reload' => TRUE,
     'membership type' => OG_MEMBERSHIP_TYPE_DEFAULT,
     'membership fields' => array(),
     'created' => time(),
    );
  • You don't need to specify entity_type nor state in the 7.x-2.x series because by default it uses the same values for those that you mention here. You just need to pass in the user entity. Check out the function definition of og_group(). I don't know if this applies in the 1.x series. – Patrick Oct 30 '15 at 15:35
0

Try the Rules module: http://drupal.org/project/rules

You could react on "User created" and assign the new user the group of the currently logged in user, which is the creator.

  • Excellent suggestion. But, I couldn't find a dynamic group selector. I could not find an "add to currently logged in users group" option. Do you know of a way round this problem? – sisko Feb 22 '12 at 21:15
  • You have "subscribe user to group" as an action. Then, in the data selector, you choose "site:current-user". Or is that not what you want? – Turion Mar 13 '13 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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