Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created September 20, 2016 07:57
Show Gist options
  • Save bangpound/28f5d7bb7b1fb723d7a13f5d2e15521b to your computer and use it in GitHub Desktop.
Save bangpound/28f5d7bb7b1fb723d7a13f5d2e15521b to your computer and use it in GitHub Desktop.
diff --git a/og_forum.module b/og_forum.module
index 4e37b94..0ccce9c 100644
--- a/og_forum.module
+++ b/og_forum.module
@@ -88,8 +88,10 @@ function og_forum_query_term_access_alter(QueryAlterableInterface $query) {
// Load all the groups of the user for which the query is executed.
$group_gids = array();
$groups = og_get_entity_groups('user', $account);
- foreach ($groups as $gid => $value) {
- $group_gids[] = $gid;
+ foreach ($groups as $group) {
+ foreach ($group as $gid) {
+ $group_gids[] = $gid;
+ }
}
// Find all instances of the {taxonomy_term_data} table being joined --
@@ -106,7 +108,7 @@ function og_forum_query_term_access_alter(QueryAlterableInterface $query) {
AND %alias.field_name = :field_name',
array(
':entity_type' => 'taxonomy_term',
- ':field_name' => 'og_group_access',
+ ':field_name' => 'og_group_ref',
));
$condition = db_or();
$condition->isNull($audience_alias . '.gid');
@@ -130,11 +132,11 @@ function og_forum_node_presave($node) {
if (_forum_node_check_node_type($node)) {
- $gids = og_get_entity_groups('taxonomy_term', taxonomy_term_load($node->taxonomy_forums[LANGUAGE_NONE][0]['tid']));
+ $groups = og_get_entity_groups('taxonomy_term', taxonomy_term_load($node->taxonomy_forums[LANGUAGE_NONE][0]['tid']));
- if (!empty($gids)) {
+ if (!empty($groups)) {
$node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE] = array();
- foreach ($gids as $group) {
+ foreach ($groups as $group) {
foreach ($group as $gid) {
$node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][] = array('target_id' => $gid);
}
@@ -150,9 +152,55 @@ function og_forum_node_presave($node) {
* Hide the OG Field on the topic form
*/
function og_forum_form_forum_node_form_alter(&$form, &$form_state) {
-
+
+ // Check to see if we are setting he default for the term
+ //_og_forum_set_node_form_term_default($form);
+
$form[OG_AUDIENCE_FIELD]['#type'] = 'hidden';
$form[OG_CONTENT_ACCESS_FIELD]['#type'] = 'hidden';
+
+}
+
+/**
+ * Internal function to set the term field default based on the og audience on the node form.
+ *
+ * This is mostly to support og_extras group content creation links.
+ *
+ * @param $form The form array
+ */
+function _og_forum_set_node_form_term_default(&$form) {
+ // If the og audience has a default value we will try to set the term's default
+ if (isset($form[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['default']['#default_value'])) {
+ if ($form[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['default']['#default_value']) {
+ // But only if we don't already have a default term value
+ if (empty($form['taxonomy_forums'][LANGUAGE_NONE]['#default_value'])) {
+ // Start by localizing a reference to the default value for groups.
+ $default_groups = $form[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['default']['#default_value'];
+ // Create a mapping of available terms, keyed by the group to which they belong
+ $group_to_terms = array();
+ foreach($form['taxonomy_forums'][LANGUAGE_NONE]['#options'] as $key_tid => $term_name) {
+ if ($key_tid != '_none') {
+ // Load the term
+ $term = taxonomy_term_load($key_tid);
+ // Note: this assumes one group per term
+ if ($term_gid = $term->og_group_ref[LANGUAGE_NONE][0]['target_id']) {
+ $group_to_terms[$term_gid] = $key_tid;
+ }
+ }
+ }
+ // Build a clean list of default term ids using the default groups and our mapping
+ $default_tids = array();
+ foreach($form[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['default']['#default_value'] as $delta => $gid) {
+ $tid = $group_to_terms[$gid];
+ if ($tid) {
+ $default_tids[] = $tid;
+ }
+ }
+ // Assign the default tids to the taxonomy_forums field
+ $form['taxonomy_forums'][LANGUAGE_NONE]['#default_value'] = $default_tids;
+ }
+ }
+ }
}
/**
@@ -171,12 +219,12 @@ function og_forum_node_access($node, $op, $account) {
$node_group = og_get_entity_groups('node', $node);
$access = NODE_ACCESS_DENY;
- if (count($node_group) == 0) {
+ if (count($node_group['node']) == 0) {
return NODE_ACCESS_ALLOW;
}
- foreach ($node_group as $gid) {
- if (in_array($gid, $user_group)) {
+ foreach ($node_group['node'] as $gid) {
+ if (in_array($gid, $user_group['node'])) {
$access = NODE_ACCESS_ALLOW;
}
}
@@ -311,7 +359,7 @@ function og_forum_og_membership_insert(OgMembership $og_membership) {
$rids = og_forum_get_roles_by_permission($og_membership->group_type, $node->type, $og_membership->gid, 'moderate');
$acl_id = _forum_access_get_acl($og_membership->etid);
- $users = og_get_users_by_roles($og_membership->gid, array_keys($roles));
+ $users = og_forum_get_users_by_roles($og_membership->gid, array_keys($roles));
$new_user = array($node->uid);
foreach ($users as $user) {
$new_user[] = $user->uid;
@@ -340,7 +388,7 @@ function og_forum_og_membership_delete(OgMembership $og_membership) {
$rids = og_forum_get_roles_by_permission($og_membership->group_type, $node->type, $og_membership->gid, 'moderate');
$acl_id = _forum_access_get_acl($og_membership->etid);
- $users = og_get_users_by_roles($og_membership->gid, array_keys($roles));
+ $users = og_forum_get_users_by_roles($og_membership->gid, array_keys($roles));
$old_user = array($node->uid);
foreach ($users as $user) {
$old_user[] = $user->uid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment