Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #701 from bboro/feature/auto-add-owners-setting
Browse files Browse the repository at this point in the history
Add 'auto_add_group_owner_membership' setting
  • Loading branch information
MPParsley authored Sep 30, 2021
2 parents a7cf6b8 + a9e38c6 commit f41da3f
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/install/og.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ group_resolvers:
- route_group_content
- request_query_argument
- user_access
auto_add_group_owner_membership: true
14 changes: 13 additions & 1 deletion config/optional/views.view.og_members_overview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -582,4 +593,5 @@ display:
- 'languages:language_interface'
- url
- url.query_args
- user.permissions
tags: { }
3 changes: 3 additions & 0 deletions config/schema/og.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions og.install
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 5 additions & 0 deletions og.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion og_ui/src/Form/AdminSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function getFormId() {
protected function getEditableConfigNames() {
return [
'og.settings',
'og_ui.settings',
];
}

Expand Down Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down
7 changes: 7 additions & 0 deletions tests/src/Kernel/Action/ActionTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
41 changes: 41 additions & 0 deletions tests/src/Kernel/GroupManagerSubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
],
];
}

}

0 comments on commit f41da3f

Please sign in to comment.