Skip to content
Snippets Groups Projects
Commit 66791fa0 authored by Eirik Morland's avatar Eirik Morland
Browse files

Issue #3345890 by eiriksm: Delete all empty teams of a user when deleting a user

parent c011f137
No related branches found
No related tags found
1 merge request!6Issue #3345890: Delete all empty teams of a user when deleting a user
......@@ -45,4 +45,25 @@ class TeamManager {
return $node_storage->loadMultiple($nids);
}
/**
* Handles user deletions.
*/
public function handleUserDelete(UserInterface $user) {
$teams = $this->getTeamsByUser($user);
if (empty($teams)) {
// Nothing to do here.
return;
}
foreach ($teams as $team) {
foreach ($team->getMemberIds() as $member) {
if ($member == $user->id()) {
continue;
}
// If it's not the same id, let's not delete this team.
continue 2;
}
$team->delete();
}
}
}
......@@ -67,9 +67,25 @@ class TeamNode extends Node {
*
* Basically a wrapper around ::referencedEntities but this is the beauty of
* entity bundle classes. They can have actual descriptive names.
*
* Please remember that this will only get actual loadable users, and not
* deleted ones, for example. Or about to be deleted.
*
* @return \Drupal\user\UserInterface[]
*/
public function getMembers() : array {
return $this->get(self::MEMBERS_FIELD)->referencedEntities();
}
/**
* Get the IDs of members in the team.
*
* This is useful so you can actually get ids of even deleted users.
*/
public function getMemberIds() : array {
return array_map(function ($item) {
return $item['target_id'];
}, $this->get(self::MEMBERS_FIELD)->getValue());
}
}
......@@ -49,6 +49,7 @@ abstract class KernelTestBase extends CoreKernelTestBase {
'field_name' => 'field_team_members',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => [
'target_type' => 'user',
],
......@@ -64,7 +65,7 @@ abstract class KernelTestBase extends CoreKernelTestBase {
],
]);
$field->save();
$this->installSchema('user', ['users_data']);
}
}
......@@ -51,4 +51,47 @@ class TeamManagerTest extends KernelTestBase {
self::assertEquals($node->id(), $team_node_result->id());
}
/**
* Test what happens when we delete a user.
*/
public function testUserDelete() {
/** @var \Drupal\violinist_teams\TeamNode $node1 */
$node1 = Node::create([
'type' => $this->nodeType->id(),
'title' => 'test1',
]);
/** @var \Drupal\violinist_teams\TeamNode $node2 */
$node2 = Node::create([
'type' => $this->nodeType->id(),
'title' => 'test2',
]);
$user1 = User::create([
'name' => 'test1',
'mail' => 'test1@example.com',
]);
$user1->save();
$user2 = User::create([
'name' => 'test2',
'mail' => 'test2@example.com',
]);
$user2->save();
// Now let's make the first one with just user 1, and the second with both.
$node1->appendMember($user1)->save();
$node2->setMembers([$user1, $user2])->save();
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
$node_storage->resetCache();
$nodes = $node_storage->loadByProperties([
'type' => $this->nodeType->id(),
]);
self::assertCount(2, $nodes);
// Now, let's delete user 1.
$user1->delete();
$node_storage->resetCache();
$nodes = $node_storage->loadByProperties([
'type' => $this->nodeType->id(),
]);
self::assertCount(1, $nodes);
}
}
......@@ -5,6 +5,8 @@
* This teams module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\UserInterface;
use Drupal\violinist_teams\TeamNode;
/**
......@@ -16,3 +18,17 @@ function violinist_teams_entity_bundle_info_alter(&$bundles) {
}
$bundles["node"][TeamNode::NODE_TYPE]['class'] = TeamNode::class;
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function violinist_teams_user_delete(EntityInterface $entity) {
if (!$entity instanceof UserInterface) {
// That should not really be possible, should it?
return;
}
/** @var \Drupal\violinist_teams\TeamManager $mng */
$mng = \Drupal::service('violinist_teams.team_manager');
$mng->handleUserDelete($entity);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment