6

How can I programmatically assign a node to an organic group?

I have tried the followings and non of them worked (assuming $target_group is the group ID that I want the $node to be assigned to):

$node->og_groups = array($target_group => $target_group);
node_save($node);

,

$group = node_load($target_group);
$node->og_groups[0] = $target_group;
$node->og_groups_names[0] = $group->title;

What is the proper way to do this?

  • which version of drupal, og are you using? – schnippy Jan 24 '12 at 19:12
2

Assuming Drupal 7, you want to use the og_group function in the API

/**
 * Set an association (e.g. subscribe) an entity to a group.
 *
 * @param $gid
 *   The group ID.
 * @param $values
 *   Array with the information to pass along, until it is processed in
 *   og_entity_presave(). Keys are:
 *   - "entity type": Optional; The entity type (e.g. "node" or "user").
 *     Defaults to 'user'
 *   - "entity" :Optional; The entity to set the association. Defaults to the
 *     current user if the $entity_type property is set to 'user'.
 *   - "state": Optional; The state of the association. Can be:
 *     - OG_STATE_ACTIVE
 *     - OG_STATE_PENDING
 *     - OG_STATE_BLOCKED
 *   - "save": Optional; TRUE if fields value should be saved. Defaults to TRUE.
 *   - "force reload": Optional; Determine if og_load_entity() should be used on
 *     the passed entity. This can be used when you pass an entity that has been
 *     saved yet to the database, but you want to assign it groups. Defaults to TRUE.
 *     // TODO: Adds docs on rest of keys.
 *
 * @return
 *   The entity with the fields updated.
 */
function og_group($gid, $values = array()) { ...

For your code, this would look like:

$values = array('entity type' => 'node', 'entity' => $node, 'state' => OG_STATE_ACTIVE);
og_group($target_group, $values);
node_save($node);
  • Thank you for your answer. I actually use Drupal 6, would that trick work on version 6 as well? – Luca Jan 25 '12 at 20:20
  • No - it won't, this method is only available in D7 og -- let me look for a D6 alternative. – schnippy Jan 25 '12 at 21:55
  • Thank you, I will flag this answer as the correct answer for D7, but it would be great if you can please tell me how this can be done in D6 as well – Luca Jan 27 '12 at 3:55
  • 2
    Sometime between when this was answered and now, the signature changed to be og_group($group_type, $gid, $values). So the solution code would look like: og_group('node', $target_group, $values) – bensnider Apr 29 '13 at 20:32
1

For Drupal 6, you can simple assign values to node object:

$gnode = node_load($gid);
$node = node_laod($nid);

$node->og_initial_groups[0][$gid];
$node->og_groups[0] = $gid;
$node->og_public = 0;
$node->og_groups_names[0] = $gnode->title;

In some cases the audience relationship doesn't store correctly if this is the case for you; you can insert a row to the og_ancestry while saving the node.

if($node = node_submit($node)) {
  $node = node_save($node);
  $sql = "INSERT INTO {og_ancestry} (nid, group_nid) VALUES (%d, %d)";
  db_query($sql, $node->nid, $gnode->nid);
}

You are done. Check in node object.

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.