2

I would like to automatically create an organic group for each newly registered user. I checked all its settings, no such option with organic groups module, then I checked the rules module but no luck there too.

1
  • why would anyone give this post negative reputation.
    – Panda
    Nov 26, 2019 at 18:48

2 Answers 2

0

The Rules module will allow you create new OG node after saving a new user account. Just do the following:

  • Create your rule and add an "After saving new user account" event to it
  • In the action section, select create a new entity
  • In the value section, select node
  • In the Content type, select the "your group"
  • Fill the title as you need; you can do this using the token
  • Fill the author

Hope this will help.

0

Ok so here is another way to do it, if you dont want to do it with rules;

    <?php
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alters the user registration form by adding a custom submit handler.
 */
function custom_user_insert(&$edit, $account, $category) {
  if (isset($account->is_new) && !empty($account->is_new)) {
        //print '<pre>';print_r($account);die;
        $groupnode = new stdClass();
        $groupnode->type = 'group'; // Group node type
        $groupnode->status = 1;
        //$groupnode->language ='de';
        $groupnode->uid =  $account->uid;
        $groupnode->title = 'group name comes here';    
        //here I save the uid from the activated user
                $groupnode->field_group_user['und'][0]['value'] = $account->uid;
        //$groupnode->{OG_GROUP_FIELD}['und'][0]['value'] = '1';

        node_save($groupnode);  
        $nid = $groupnode->nid;
        og_group('node',$nid, array(
        //"entity type"       => 'user', // defaults to user
        "entity"        => $account->uid,
        'field_name' => 'og_user_node',
        'state' => 1,//OG_STATE_ACTIVE
        'force_reload' => TRUE
        ));
        //drupal_set_message('Do it!');
        // Changes the users role in the group (1 = non-member, 2 = member, 3 = administrator member)
        og_role_grant('node',$nid, $account->uid, 12);

        $assign_grp_userid=loadusers_by_role('coaching expert');

         og_group('node',$nid, array(
        //"entity type"       => 'user', // defaults to user
        "entity"        => $assign_grp_userid,
        'field_name' => 'og_user_node',
        'state' => 1,//OG_STATE_ACTIVE
        'force_reload' => TRUE
        ));
        //drupal_set_message('Do it!');
        // Changes the users role in the group (1 = non-member, 2 = member, 3 = administrator member)
        og_role_grant('node',$nid, $account->uid, 11); 
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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