User login

Organic Groups in Drupal 7 does not use entity IDs as group IDs

Update: Here is the code we currently use, abstracted into a helper function with lots of comments but it's really just two lines:

<?php
/**
 * Get the group ID for a given node.
 *
 * Helper function for Organic Groups: an easy way toget a gid without loading
 * a full group.  Borrowed from within og_get_group().
 */
function agaricutility_get_gid($nid) {
 
// Note: Caching is handled within og_get_group_ids().
 
if ($gids = og_get_group_ids('node', array($nid))) {
   
// We don't use the entity ID directly, as it might change. For example, if
    // a node is a translation of another node that is a group, we need to load
    // the other node. og_get_group_ids() returns the correct entity ID as the
    // key, so we will use that.
   
$correct_etid = key($gids);
  }
  return
$gids[$correct_etid];
}
?>

Background

Instead of re-using entity IDs (such as node ID) as group IDs, in Drupal 7 Organic Groups has its own group ID sequence.

You can see this quite plainly in the {og} table (after a debugger has prompted you to realize that the gid was 2 instead of 345).

mysql> select * from og;
+-----+------+-------------+---------------------------------------------------+-------+------------+
| gid | etid | entity_type | label                                             | state | created    |
+-----+------+-------------+---------------------------------------------------+-------+------------+
|   1 |  357 | node        | This is a something or other                      |     1 | 1313484117 |
|   2 |  345 | node        | Dream Hub Yongsan International Business District |     1 | 1313492550 |
+-----+------+-------------+---------------------------------------------------+-------+------------+
2 rows in set (0.00 sec)

Yet looking at node 345 in a debugger, it has an entry in line with all the field_*s called group_group, with value [und][0][value] equal to 1... instead of 2. Weird. Currently i trust the database more. Ah! That field thing is just whether the node is a group or not, nothing to do with its ID.

There is no Views relationship for connecting a member of its group to the group, unless you already know this secret group ID. Ridiculous. (When starting with a files-based View, anyway; there's a @TODO about doing all entities.

og_get_group_ids() technically has the matching group ID and node ID ("entity ID") in it, keyed by entity ID. The API function to use is:

og_get_group($entity_type, $etid[, optional stuff])

For example, to get the group ID of a node you can do:

<?php
  $group
= og_get_group('node', $node->nid);
 
$gid = $group->gid;
?>
Searched words: 
d7 og groups nids organic groups drupal 7 gid

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • 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> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

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