Skip to content

Instantly share code, notes, and snippets.

@dsnopek
Created January 16, 2014 14:15
Show Gist options
  • Save dsnopek/8455651 to your computer and use it in GitHub Desktop.
Save dsnopek/8455651 to your computer and use it in GitHub Desktop.
Some EntityFieldQuery's for working with OG subgroups.
<?php
/**
* Gets all content contained in a group and all it's sub-groups.
*
* @param integer $gid
* The OG group gid (the nid of the node).
*
* @return array
* An array of node ids.
*/
function example_get_all_content($gid) {
$content = array();
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'og_membership')
->propertyCondition('entity_type', 'node', '=')
->propertyCondition('group_type', 'node', '=')
->propertyCondition('gid', $gid, '=')
->propertyCondition('state', OG_STATE_ACTIVE, '=');
$result = $query->execute();
if (!empty($result['og_membership'])) {
$memberships = og_membership_load_multiple(array_keys($result['og_membership']));
foreach ($memberships as $membership) {
$content[] = $membership->etid;
if ($membership->field_name == 'oa_parent_space') {
$content = array_merge($content, example_get_all_content($membership->etid));
}
}
}
return $content;
}
/*
* Here is a random other one..
*/
// Find all the user's group memberships.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'og_membership')
->propertyCondition('entity_type', 'user', '=')
->propertyCondition('etid', $account->uid, '=')
->propertyCondition('group_type', 'node', '=')
->propertyCondition('state', OG_STATE_ACTIVE, '=');
$result = $query->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment