RSS

Newsletter

The CodeKarate Newsletter is the best way for technical ninjas to keep their swords sharpened. Don't worry, we won't flood your inbox and your email address will always remain private.
Back to Top

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

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <blockquote> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <php>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.