diff --git a/includes/og.field.inc b/includes/og.field.inc index 189afd5..afccaba 100644 --- a/includes/og.field.inc +++ b/includes/og.field.inc @@ -63,18 +63,19 @@ function og_field_widget_form(&$form, &$form_state, $field, $instance, $langcode $field_modes[] = 'admin'; } - // Get the "Other group" group IDs. - $target_type = $field['settings']['target_type']; - $other_groups_ids = array(); - if ($id) { - $entity_gids = og_get_entity_groups($entity_type, $entity); - $entity_gids = !empty($entity_gids[$target_type]) ? $entity_gids[$target_type] : array(); + // Build an array of entity IDs. Field's $items are loaded + // in OgBehaviorHandler::load(). + $entity_gids = array(); + foreach ($items as $item) { + $entity_gids[] = $item['target_id']; + } - $user_gids = og_get_entity_groups(); - $user_gids = !empty($user_gids[$target_type]) ? $user_gids[$target_type] : array(); + $target_type = $field['settings']['target_type']; + $user_gids = og_get_entity_groups(); + $user_gids = !empty($user_gids[$target_type]) ? $user_gids[$target_type] : array(); - $other_groups_ids = array_diff($entity_gids, $user_gids); - } + // Get the "Other group" group IDs. + $other_groups_ids = array_diff($entity_gids, $user_gids); foreach ($field_modes as $field_mode) { $mocked_instance = og_get_mocked_instance($instance, $field_mode); diff --git a/og.test b/og.test index 5bd303a..6c9ffd0 100644 --- a/og.test +++ b/og.test @@ -928,7 +928,6 @@ class OgComplexWidgetTestCase extends DrupalWebTestCase { og_create_field(OG_AUDIENCE_FIELD, 'node', 'post', $og_field); } - /** * Test "field modes" of the OG reference widget. */ @@ -1017,8 +1016,6 @@ class OgComplexWidgetTestCase extends DrupalWebTestCase { $user1 = $this->drupalCreateUser(); $user2 = $this->drupalCreateUser(); - $type = $this->drupalCreateContentType(); - $settings = array( 'type' => 'group', OG_GROUP_FIELD . '[und][0][value]' => 1, @@ -1033,6 +1030,43 @@ class OgComplexWidgetTestCase extends DrupalWebTestCase { $this->assertTrue(og_get_entity_groups('user', $user2, array(OG_STATE_PENDING)), 'User membership was retained after user save.'); } + + /** + * Test multiple group-audience fields. + */ + function testMultipleFields() { + // Add another group-audience field. + $og_field = og_fields_info(OG_AUDIENCE_FIELD); + og_create_field('another_field', 'node', 'post', $og_field); + + $user1 = $this->drupalCreateUser(); + + // Create a group. + $settings = array( + 'type' => 'group', + OG_GROUP_FIELD . '[und][0][value]' => 1, + 'uid' => $user1->uid, + ); + $group1 = $this->drupalCreateNode($settings); + $group2 = $this->drupalCreateNode($settings); + + // Create group content. + $settings = array( + 'type' => 'post', + 'uid' => $user1->uid, + ); + $post1 = $this->drupalCreateNode($settings); + + og_group('node', $group1->nid, array('entity_type' => 'node', 'entity' => $post1, 'field_name' => OG_AUDIENCE_FIELD)); + og_group('node', $group2->nid, array('entity_type' => 'node', 'entity' => $post1, 'field_name' => 'another_field')); + + $this->drupalLogin($user1); + $this->drupalGet("node/$post1->nid/edit"); + + // Assert correct selection in both fields. + $this->assertOptionSelected('edit-og-group-ref-und-0-default', $group1->nid); + $this->assertOptionSelected('edit-another-field-und-0-default', $group2->nid); + } } /**