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

Drupal 7 Add Node to Organic Group (OG) programmatically

Needed an easy way to add content into an organic group using Drupal 7. I ended up using the og_group function provided by Organic Groups (in the og.module file). Here is the quick solution that worked for me.

  //Get the group id
  $gid = 25;
 
  //Get the full node that needs to be subscribed to the group
  $node = node_load(17);
 
  //Set the values
  $values = array(
    'entity_type' => 'node',
    'entity' => $node,
    'state' => OG_STATE_ACTIVE,
  );
 
  //Add this node to the group
  $result = og_group('node', $gid, $values);

Discussions

1
Niraj (not verified)

how do we get the $gid

how do we get the $gid

1-1
shane

$gid value

The $gid value is just the node id of the group node. If you go to the group page and click on the edit link for that group content, you can pull the node id (also the $gid) from the URL. You can also get this value from the node table in the database.

shane's picture
2
Niraj (not verified)

I assume that the the content

I assume that the the content type has to be a group to do this does the above code change the group_group field value to ON as well or should we do that manually

or can we update the node's group_group field and save the node

2-1
shane

Group content type

I believe the content type needs to be a group in order for this to work. The content you are posting into the group should also be set up so that the content can be posted into the group. I believe all this is configuration on the content type settings page or each content type.

shane's picture
3
天空蔚蓝 (not verified)

didn't work for me,

needn't node_save()?
here is my code:
$group_type ='node';
$gid = 104;
$entity = node_load(274);
$values = array(
'entity type' => 'node',
'entity' => $entity);
//og_is_member($group_type, $gid, $entity_type = 'user', $entity = NULL, $states = array(OG_STATE_ACTIVE))
$is_member_before = og_is_member($group_type, $gid, $entity_type = 'node', $entity, $states = array(OG_STATE_ACTIVE)) ;
dpm('is_member_before', var_export($is_member_before,true));

og_group('node',$gid, $values);

//function og_group($group_type, $gid, $values = array())
node_save($entity);
$is_member_before = og_is_member($group_type, $gid, $entity_type = 'node', $entity, $states = array(OG_STATE_ACTIVE)) ;
dpm('is_member_after', var_export($is_member_before,true));

4
yool (not verified)

organic group module

Hi Shane,
I'm really enjoying the daily dose of drupal videos!
May you please cover organic group 7.2 module and group layout setup?
I have seen multiple videos on that topic but all were pretty confusing for me.
Thanks, Yool

5
Stox (not verified)

How would you do this when

How would you do this when creating a node programmatically and don't know the nid?

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.