diff --git a/config/install/og.settings.yml b/config/install/og.settings.yml index 32cf568ce..664843835 100644 --- a/config/install/og.settings.yml +++ b/config/install/og.settings.yml @@ -9,3 +9,4 @@ group_resolvers: - route_group_content - request_query_argument - user_access +auto_add_group_owner_membership: true diff --git a/config/optional/views.view.og_members_overview.yml b/config/optional/views.view.og_members_overview.yml index d91b7da66..1f1e7a79a 100644 --- a/config/optional/views.view.og_members_overview.yml +++ b/config/optional/views.view.og_members_overview.yml @@ -478,7 +478,18 @@ display: sorts: { } header: { } footer: { } - empty: { } + empty: + area_text_custom: + id: area_text_custom + table: views + field: area_text_custom + relationship: none + group_type: group + admin_label: 'No results text' + empty: true + tokenize: false + content: 'There are no members that belong to this group.' + plugin_id: text_custom relationships: uid: id: uid @@ -582,4 +593,5 @@ display: - 'languages:language_interface' - url - url.query_args + - user.permissions tags: { } diff --git a/config/schema/og.schema.yml b/config/schema/og.schema.yml index 171c7b39e..06098017e 100644 --- a/config/schema/og.schema.yml +++ b/config/schema/og.schema.yml @@ -39,6 +39,9 @@ og.settings: sequence: type: string label: 'OgGroupResolver plugin ID.' + auto_add_group_owner_membership: + type: boolean + label: 'Automatically add creators to the group' og.settings.group.*: type: sequence diff --git a/og.install b/og.install index 134934e10..0622ac0dc 100644 --- a/og.install +++ b/og.install @@ -93,3 +93,13 @@ function og_update_8002() { $manager->updateEntityType($entity_type); $manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition('uuid', 'og_membership')); } + +/** + * Add auto_add_group_owner_membership configuration to og.settings config. + */ +function og_update_8003(&$sandbox) { + $config = \Drupal::configFactory()->getEditable('og.settings'); + // This setting defaults to TRUE for backwards compatibility. + $config->set('auto_add_group_owner_membership', TRUE); + $config->save(TRUE); +} diff --git a/og.module b/og.module index fb878a92a..9f7ed92ae 100755 --- a/og.module +++ b/og.module @@ -35,6 +35,11 @@ function og_entity_insert(EntityInterface $entity) { // Invalidate cache tags if new group content is created. og_invalidate_group_content_cache_tags($entity); + if (!\Drupal::config('og.settings')->get('auto_add_group_owner_membership')) { + // Disabled automatically adding group creators to the group. + return; + } + if (!Og::isGroup($entity->getEntityTypeId(), $entity->bundle())) { // Not a group entity. return; diff --git a/og_ui/src/Form/AdminSettingsForm.php b/og_ui/src/Form/AdminSettingsForm.php index 710d68a1c..5b0c5b044 100644 --- a/og_ui/src/Form/AdminSettingsForm.php +++ b/og_ui/src/Form/AdminSettingsForm.php @@ -58,7 +58,6 @@ public function getFormId() { protected function getEditableConfigNames() { return [ 'og.settings', - 'og_ui.settings', ]; } @@ -133,6 +132,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { } } + $form['auto_add_group_owner_membership'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Automatically add creators to the group'), + '#description' => $this->t('When a user creates a group, they automatically become a member of the group.'), + '#default_value' => $config_og->get('auto_add_group_owner_membership'), + ]; + $form['#attached']['library'][] = 'og_ui/form'; return $form; @@ -149,6 +155,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { ->set('node_access_strict', $form_state->getValue('og_node_access_strict')) ->set('delete_orphans', $form_state->getValue('og_delete_orphans')) ->set('delete_orphans_plugin_id', $form_state->getValue('og_delete_orphans_plugin_id')) + ->set('auto_add_group_owner_membership', $form_state->getValue('auto_add_group_owner_membership')) ->save(); } diff --git a/tests/src/Kernel/Action/ActionTestBase.php b/tests/src/Kernel/Action/ActionTestBase.php index f665fdfc7..5c9028ca5 100644 --- a/tests/src/Kernel/Action/ActionTestBase.php +++ b/tests/src/Kernel/Action/ActionTestBase.php @@ -106,6 +106,13 @@ protected function setUp(): void { ])->save(); $this->groupTypeManager->addGroup('node', $group_bundle); + // Set OG to automatically create memberships for group owners. This is done + // because we're not installing OG config in this Kernel test. + $config = $this->container->get('config.factory')->getEditable('og.settings'); + $config->set('auto_add_group_owner_membership', TRUE); + $config->save(TRUE); + $this->assertTrue($config->get('auto_add_group_owner_membership')); + // Create a test group. $this->group = Node::create([ 'title' => $this->randomString(), diff --git a/tests/src/Kernel/GroupManagerSubscriptionTest.php b/tests/src/Kernel/GroupManagerSubscriptionTest.php index 0717a531a..d4a0880a4 100644 --- a/tests/src/Kernel/GroupManagerSubscriptionTest.php +++ b/tests/src/Kernel/GroupManagerSubscriptionTest.php @@ -118,6 +118,27 @@ public function testGroupManagerSubscription($group_has_owner, $membership_is_ov $this->assertEquals($membership_is_overridden, $this->isMembershipOverridden($membership)); } + /** + * Tests 'Automatically add creators to the group' admin settings. + * + * @dataProvider autoAddOwnersSettingProvider + */ + public function testAutoAddOwnersSetting($auto_add_group_owner_membership) { + $config = $this->container->get('config.factory')->getEditable('og.settings'); + $config->set('auto_add_group_owner_membership', $auto_add_group_owner_membership); + $config->save(); + $group = Node::create([ + 'title' => 'membership is not overridden', + 'type' => 'group', + 'uid' => $this->owner->id(), + ]); + $group->save(); + $membership = $this->membershipManager->getMembership($group, $this->owner->id()); + // Membership should be created only when auto_add_group_owner_membership is + // enabled. + $this->assertEquals($auto_add_group_owner_membership, !empty($membership)); + } + /** * Checks if the membership is overridden by a custom hook implementation. * @@ -177,4 +198,24 @@ public static function groupManagerSubscriptionProvider() { ]; } + /** + * Data provider for ::testAutoAddOwnersSetting(). + * + * @return array[] + * Array of data with values for the 'auto_add_group_owner_membership' + * config key. + */ + public static function autoAddOwnersSettingProvider() { + return [ + [ + // Auto add owners. + TRUE, + ], + [ + // Don't auto add owners. + FALSE, + ], + ]; + } + }