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

Issue #3354163: Make it possible to get all admins of a team

parent 4c279d0f
No related branches found
No related tags found
1 merge request!8Issue #3354163: Make it possible to get all admins of a team
......@@ -12,6 +12,7 @@ class TeamNode extends Node {
const NODE_TYPE = 'team';
const MEMBERS_FIELD = 'field_team_members';
const ADMIN_FIELD = 'field_team_admins';
/**
* Get the team plan.
......@@ -31,6 +32,62 @@ class TeamNode extends Node {
}
}
/**
* Check if a user is an admin of the team.
*/
public function isAdmin(UserInterface $user) {
if (!$user->id()) {
return FALSE;
}
return in_array($user->id(), $this->getAdministrators());
}
/**
* Get all of the admins of a team.
*
* Will return an array of user ids.
*/
public function getAdministratorIds() : array {
if (!$this->hasField(self::ADMIN_FIELD) || $this->get(self::ADMIN_FIELD)->isEmpty()) {
return [];
}
return array_map(function ($item) {
return $item["target_id"];
}, $this->get(self::ADMIN_FIELD)->getValue());
}
/**
* Get all of the admins of a team, the actual user objects.
*/
public function getAdministrators() : array {
if (!$this->hasField(self::ADMIN_FIELD) || $this->get(self::ADMIN_FIELD)->isEmpty()) {
return [];
}
return $this->get(self::ADMIN_FIELD)->referencedEntities();
}
/**
* Append an admin.
*
* If you are trying to append a user that is already in there, it will only
* be referenced once.
*/
public function appendAdmin(UserInterface $user) : self {
if (!$user->id()) {
throw new \InvalidArgumentException('Can not reference user id 0 when adding admins');
}
$admins = $this->getAdministrators();
$already_has_it = (bool) array_filter($admins, function (UserInterface $user_item) use ($user) {
return $user->id() === $user_item->id();
});
if ($already_has_it) {
return $this;
}
$admins[] = $user;
$this->setAdmins($admins);
return $this;
}
/**
* Append a member.
*
......@@ -62,6 +119,15 @@ class TeamNode extends Node {
return $this;
}
/**
* Set all of the admins of the node.
*/
public function setAdmins(array $admins) : self {
$this->get(self::ADMIN_FIELD)
->setValue($admins);
return $this;
}
/**
* Helper to get all members of this team.
*
......
......@@ -65,6 +65,28 @@ abstract class KernelTestBase extends CoreKernelTestBase {
],
]);
$field->save();
// Create the field for holding admins.
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_team_admins',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => [
'target_type' => 'user',
],
]);
$fieldStorage->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => $this->nodeType->id(),
'field_type' => 'entity_reference',
'required' => FALSE,
'settings' => [
['handler' => 'default:user'],
],
]);
$field->save();
$this->installSchema('user', ['users_data']);
}
......
......@@ -41,4 +41,22 @@ class TeamNodeTest extends KernelTestBase {
self::assertCount(1, $team->getMembers());
}
/**
* Tests getting admins from a node.
*/
public function testGetAdmins() {
/** @var \Drupal\violinist_teams\TeamNode $node */
$node = Node::create([
'type' => $this->nodeType->id(),
]);
$user = User::create([
'name' => 'test',
'mail' => 'test@example.com',
]);
$user->save();
self::assertEquals([], $node->getAdministrators());
$node->appendAdmin($user);
self::assertEquals([$user->id()], $node->getAdministratorIds());
}
}
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