9

I have this node that belongs to a group, and I want to get the group ID of that node, to print it, something like $node->nid; but with the group id.

Is that possible?

11

The relationship between group content and a group is via a membership relation. Since it is possible for group content to be a member of multiple groups then the following code will retrieve the list of groups:

$groups = og_get_entity_groups('node', $node);

This is a multidimensional array with the group entity_types as the key and memberships as the value. Each membership is an array with the membership id as they key and then group's entity id as the value. So to go from a group content node, to all it's group parents you would use ...

foreach (og_get_entity_groups('node', $node) as $entity_type => $og_memberships) {
  foreach ($og_memberships as $membership_id => $entity_id) {
    if ($entity_type == 'node') {
      // Assuming we only want to deal with node groups, we can
      // access the group node with...
      $group_node = node_load($entity_id);          
    }
  }
}
4

I needed this last night. Did it like this:

$node = node_load($nid);
$og_id = current($node->og_groups)

$og_groups contains an array of all the OG that a post is a member of. You could get them all with a foreach. Te key to access them is the nid.

$og_groups=array(5=>5, 15=>15); 

Hope that helps.

3

The og(organic groups) module add entity refrence field to content types you want be a group or be a group content. by default name of field is og_group_ref but maybe it change to another name. If you want exactlly find out og field name please see the your content type manage field (lets suppose your considered article content type ) admin/structure/types/manage/article/fields.
Pick up group field MACHINE NAME (some thing like og_group_ref).

Your can something like this :

$node=node_load($nid);
$ogId=$node->og_group_ref['und'][0]['target_id'];
2

Find out the gid then try print the whole $node object, something like:

  print_r($node);  

Some reason Drupal answers will not display anything in the php tags, but you should wrap the php in pre to make the output more readable.

Try ctrl + F and find where the number is outputted. You can then follow the format of the arrays / objects to print it when $node is available.

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.