5

How would you programmatically create an OG group? I have tried node_save() but to no joy.

3
  • 1
    Which Drupal version? Jun 21, 2011 at 20:37
  • 6. Tag added to question.
    – Lucy
    Jun 21, 2011 at 21:18
  • Hmmm... looking at the og_nodeapi function, it looks like node_save should do the trick. I can't see why it doesn't work. Just a thought: maybe you can try building a $form_state array and run drupal_execute. Jun 21, 2011 at 23:24

3 Answers 3

5

It's simpler than attaching node as group post:

  <?php
  global $user;
  $node = new stdClass();
  $node->type = 'group'; // Change this to group node type name.
  $node->uid = $user->uid;
  $node->title = t('Title'); // Change title generation
  $node->body = t('Body'); // Change body generation
  $node->og_description = t('Group description'); // Change group description generation
  $node->og_selective = OG_CLOSED;
  $node->og_register = OG_REGISTRATION_NEVER;
  $node->og_directory = OG_DIRECTORY_CHOOSE_FALSE;
  $node->og_private = 0;
  node_save($node);
  og_insert_group($node);
  og_save_subscription($node->nid, $node->uid, array('is_active' => 1, 'is_admin' => 1));

(ofcourse you also need to check the return values from alle the three functions)

1

I don't really understand the question, but I created a lot of groups using the Mass Create module.

1
  • well laxman13 and Reg Gordon - thx for the answer. I try to do it like you. I come back and report all my findings!
    – zero
    Mar 27, 2012 at 15:34
1

I was in the same struggle, but I realized that to create a new OG group programatically, the Rules module could be the key.

If you look for the right trigger and conditions, the first action would be "Entities > Create a new entity >OG group.

You can use the Rules scheduler to do the timing, if necessary.

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.