Hi guys,

I'm a php programmer by trade and have built a few drupal-based sites in the past. Now I'm looking into drupal modules in more depth.

What I need to do is create an organic group when a new 'client' user account is created. This group will eventually have other users of role 'employee' assigned to them, although this will be done manually by the administrator. The roles stuff I can work out, it's the creation of the organic group where I am having difficulty.

So far I've worked out that I need to create a hook_user function with $op set to 'insert'. I also think I need to send data to og_nodeapi() with $op = 'insert' from the user hook.

How would I go about creating the form data for the group insert in a 'drupal approved' manner?

Any help is greatly appreciated.

Regards,

Sam.

Comments

nevets’s picture

I have created a custom module to work as an organic group called homepage, It's user hook looks like

/**
 * Implementation of hook_user().
 */
function homepage_user($op, &$edit, &$account, $category=NULL) {
 	if ( ! $account->uid ) {
 		return;
 	}
 	
 	switch ( $op ) {
 	case 'update':
 		// Check for an existing group for this user
 		$sql = "SELECT nid FROM {node} WHERE type = 'homepage' AND uid = %d";
 		$results = db_query($sql, $account->uid);
 		if ( db_num_rows($results) > 0 ) {
 			// We found a home page - return
 			return;
 		}
 		// Fall through to insert case
 		
 	case 'insert':
 		homepage_create($account);
 		break;
 	}
 			
}

The update case is there because there where some existing login, it may not be needed in your case, if you use it, change 'homepage' in the sql to reflect the type you are using. Then homepage_create looks like

function homepage_create($account) {
 	$node = new StdClass();
 	
 	$node->title = "Some title for node/group";
 	
 	$node->uid = $account->uid;
 	$node->type = 'homepage';   // This again needs to match the type you are using for the group
	$node->name = $account->name;
	$node->status = 1;
	
	$node->og_selective = 0;
	$node->og_description = "The default description";
	$node->og_directory = 1;
	
	node_save($node);
	
	drupal_set_message("New group for $account->name created");
}
samhassell’s picture

Thanks for the info, I'll post my results soon.

samhassell’s picture

thanks a heap, this worked perfectly for my needs!