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;
?>
Comments
Post new comment