Skip to content
Snippets Groups Projects
Commit 6e9eb78c authored by Nikolay Lobachev's avatar Nikolay Lobachev
Browse files

Issue #3400030: Add option to remove group content when user join the group

parent 8a49cc7d
No related branches found
No related tags found
1 merge request!33Issue #340030: Add option to remove group invitaiton, when use join group, update tests.
......@@ -22,7 +22,11 @@ group_content_enabler.config.existing_user_invitation_body:
type: 'text'
label: 'Body'
group_relation.config.autoaccept_invitees:
group_content_enabler.config.remove_invitation:
type: 'integer'
label: 'Remove an invitation, when user join the group.'
group_content_enabler.config.autoaccept_invitees:
type: 'boolean'
label: 'Automatically accept invitation'
......
......@@ -147,9 +147,16 @@ function ginvite_group_content_insert(GroupContentInterface $group_content) {
$invitations = \Drupal::service('ginvite.invitation_loader')->loadByProperties($properties);
if (!empty($invitations)) {
foreach ($invitations as $invitation) {
// Mark invitation as accepted.
$invitation->getGroupContent()->set('invitation_status', GroupInvitation::INVITATION_ACCEPTED)->save();
// Remove group content if setting is enabled.
if ($invitation->getGroupContent()->getContentPlugin()->getConfiguration()['remove_invitation'] == 1) {
$invitation->getGroupContent()->delete();
}
else {
// Mark invitation as accepted.
$invitation->getGroupContent()->set('invitation_status', GroupInvitation::INVITATION_ACCEPTED)->save();
}
}
$messenger->addMessage(t('You have accepted the invitation.'));
......@@ -166,8 +173,7 @@ function ginvite_group_content_delete(GroupContentInterface $group_content) {
if ($group_content->getContentPlugin()->getPluginId() == 'group_invitation') {
$group = $group_content->getGroup();
// Load plugin configuration.
$group_plugin_collection = \Drupal::service('plugin.manager.group_content_enabler')->getInstalled($group->getGroupType());
$group_invite_config = $group_plugin_collection->getConfiguration()['group_invitation'];
$group_invite_config = $group_content->getContentPlugin()->getConfiguration();
if ($group_invite_config['send_cancel_email'] && $group_content->get('invitation_status')->value == GroupInvitation::INVITATION_PENDING) {
$from = $group_content->getEntity();
......@@ -184,6 +190,19 @@ function ginvite_group_content_delete(GroupContentInterface $group_content) {
}
}
}
if ($group_content->getContentPlugin()->getPluginId() == 'group_membership') {
$properties = [
'entity_id' => $group_content->getEntity()->id(),
'gid' => $group_content->getGroup()->id(),
];
// Remove invitations, when we remove the user.
$invitations = \Drupal::service('ginvite.invitation_loader')->loadByProperties($properties);
foreach ($invitations as $invitation) {
$invitation->getGroupContent()->delete();
}
}
}
/**
......
services:
ginvite_event_subscriber:
class: Drupal\ginvite\EventSubscriber\GinviteSubscriber
tags:
- {name: event_subscriber}
arguments: ['@ginvite.invitation_loader', '@current_user', '@messenger', '@logger.factory', '@config.factory']
ginvite.invitation_loader:
class: 'Drupal\ginvite\GroupInvitationLoader'
arguments: ['@entity_type.manager', '@current_user']
......@@ -82,21 +82,19 @@ class InvitationOperations extends ControllerBase {
*/
public function accept(Request $request, GroupContentInterface $group_content) {
$group = $group_content->getGroup();
$contentTypeConfigId = $group_content->getGroup()
->getGroupType()
$group_type = $group->getGroupType();
$content_type_config_id = $group_type
->getContentPlugin('group_membership')
->getContentTypeConfigId();
// Load invitation plugin configuration.
$invitation_plugin_configuration = $group_content->getGroup()
->getGroupType()
$invitation_plugin_configuration = $group_type
->getContentPlugin('group_invitation')
->getConfiguration();
// Pre-populate a group membership with the current user.
$group_membership = GroupContent::create([
'type' => $contentTypeConfigId,
'type' => $content_type_config_id,
'entity_id' => $group_content->get('entity_id')->getString(),
'content_plugin' => 'group_membership',
'gid' => $group->id(),
......@@ -113,9 +111,9 @@ class InvitationOperations extends ControllerBase {
// Try to honor the destination parameter, fallback to the group route.
if ($request->query->has('destination')) {
$dest = $request->get('destination');
$destination = $request->get('destination');
try {
$path = Url::fromUserInput($dest)->setAbsolute()->toString();
$path = Url::fromUserInput($destination)->setAbsolute()->toString();
return new RedirectResponse($path);
}
catch (\InvalidArgumentException $e) {
......@@ -152,9 +150,9 @@ class InvitationOperations extends ControllerBase {
$this->messenger->addMessage($this->t('You have declined the @group_bundle invitation.', ['@group_bundle' => $group_bundle]));
if ($request->query->has('destination')) {
$dest = $request->get('destination');
$destination = $request->get('destination');
try {
$path = Url::fromUserInput($dest)->setAbsolute()->toString();
$path = Url::fromUserInput($destination)->setAbsolute()->toString();
return new RedirectResponse($path);
}
catch (\InvalidArgumentException $e) {
......
......@@ -182,6 +182,7 @@ class GroupInvitation extends GroupContentEnablerBase implements ContainerFactor
'cancel_user_invitation_body' => $body_message_cancel,
'send_cancel_email' => FALSE,
'invitation_bypass_form' => FALSE,
'remove_invitation' => FALSE,
];
}
......@@ -329,6 +330,13 @@ class GroupInvitation extends GroupContentEnablerBase implements ContainerFactor
'#default_value' => $configuration['invitation_bypass_form'],
];
$form['remove_invitation'] = [
'#type' => 'checkbox',
'#title' => $this->t('Remove invitation'),
'#description' => $this->t('Remove an invitation when a user join a group.'),
'#default_value' => $configuration['remove_invitation'],
];
$form['invitation_expire'] = [
'#type' => 'number',
'#title' => $this->t('Expire invites'),
......
<?php
namespace Drupal\Tests\ginvite\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
/**
* Tests that all config provided by this module passes validation.
*
* @group ginvite
*/
class GroupInviteConfigTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'views',
'group',
'options',
'entity',
'variationcache',
'ginvite',
];
/**
* Tests that the module's config installs properly.
*/
public function testConfig() {
$this->installConfig(['ginvite']);
}
}
<?php
namespace Drupal\Tests\ginvite\Kernel;
use Drupal\group\Entity\GroupContent;
use Drupal\Tests\group\Kernel\GroupKernelTestBase;
/**
* Tests the general behavior of group content group_invitation.
*
* @group ginvite
*/
class GroupInviteTest extends GroupKernelTestBase {
/**
* The invitation loader.
*
* @var \Drupal\ginvite\GroupInvitationLoaderInterface
*/
protected $invitationLoader;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The group we will use to test methods on.
*
* @var \Drupal\group\Entity\Group
*/
protected $group;
/**
* The group content type for group membership request.
*
* @var \Drupal\group\Entity\GroupContentTypeInterface
*/
protected $groupContentType;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'ginvite',
'user',
'system',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installSchema('user', ['users_data']);
$this->installEntitySchema('user');
$this->installConfig([
'ginvite',
]);
$this->invitationLoader = $this->container->get('ginvite.invitation_loader');
$this->entityTypeManager = $this->container->get('entity_type.manager');
$this->group = $this->createGroup();
$config = [
'group_cardinality' => 0,
'entity_cardinality' => 1,
'remove_invitation' => 0,
];
// Enable group membership request group content plugin.
$this->groupContentType = $this->entityTypeManager->getStorage('group_content_type')->createFromPlugin($this->group->getGroupType(), 'group_invitation', $config);
$this->groupContentType->save();
}
/**
* Test group invitation removal with disabled settings.
*/
public function testRequestRemovalWithDisabledSettings() {
$account = $this->createUser();
// Add an invitation.
$this->createInvitation($this->group, $account);
// Add the user as member.
$this->group->addMember($account);
// Since removal is enabled we should not find any invitations.
$group_invitations = $this->invitationLoader->loadByProperties([
'gid' => $this->group->id(),
'entity_id' => $account->id(),
]);
$this->assertCount(1, $group_invitations);
}
/**
* Test group invitation removal with enabled settings.
*/
public function testInvitationRemovalWithEnabledSettings() {
$config = [
'group_cardinality' => 0,
'entity_cardinality' => 1,
'remove_invitation' => 1,
];
$this->groupContentType->updateContentPlugin($config);
$account = $this->createUser();
// Add an invitation.
$this->createInvitation($this->group, $account);
// Add the user as member.
$this->group->addMember($account);
// Since removal is enabled we should not find any invitations.
$group_invitations = $this->invitationLoader->loadByProperties([
'gid' => $this->group->id(),
'entity_id' => $account->id(),
]);
$this->assertCount(0, $group_invitations);
}
/**
* Test autoacception of invitations.
*/
public function testInvitationAutoAcception() {
$config = [
'group_cardinality' => 0,
'entity_cardinality' => 1,
'autoaccept_invitees' => 1,
];
$this->groupContentType->updateContentPlugin($config);
$account = $this->createUser();
// Add an invitation.
$this->createInvitation($this->group, $account);
// It will call the same function, which is called during the login.
$account->save();
$member = $this->group->getMember($account);
$this->assertNotNull($member);
}
/**
* Creates group invitation.
*
* @param \Drupal\group\Entity\Group $group
* Group.
* @param \Drupal\user\UserInterface $user
* User.
*
* @return \Drupal\group\Entity\GroupContent
* Group content invitation.
*/
private function createInvitation($group, $user) {
$group_content = GroupContent::create([
'type' => $group
->getGroupType()
->getContentPlugin('group_invitation')
->getContentTypeConfigId(),
'gid' => $group->id(),
'entity_id' => $user->id(),
'invitee_mail' => $user->get,
]);
$group_content->save();
return $group_content;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment