Creating an Organic Groups Group Node Programatically
I recently ran into a problem on a site using the Organic Groups module in which I had to automatically create a group node when a user purchased a product with Ubercart. Below is the function I created to automatically create that Group node.
/** * Creates an empty group * * @param $name * The name of the group to create * @param $user * The user to make the group owner */ function MYMODULE_create_group($name, $user) { $node = new stdClass(); $node->type = 'MY_GROUP_TYPE'; $node->title = $name; $node->uid = $user->uid; $node->name = $user->name; $node->comment = 0; $node->promote = 0; //Organic groups fields $node->og_description = 'A group managed by ' . $user->name; $node->og_register = 0; $node->og_directory = 0; $node->og_private = 1; $node->og_selective = 3; //create the node $node = node_submit($node); node_save($node); }
The various "og" settings that are set at the end can be configured to create different types of groups. You could easily enhance the function to accept additional parameters for those functions, but I only required one type of group on the site I was building (Private groups that are not available on the registration page, and not listed in the directory).
In the example above, you will need to replace MYMODULE with the name of your module and MY_GROUP_TYPE with the content type of the group node you want to create. You should then be able to call this function anytime you need a group created for a user.
Hopefully this is useful... but either way, let me know in the comments.



Discussions
Post new comment