View source
<?php
namespace Drupal\apigee_edge_teams\Controller;
use Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface;
use Drupal\apigee_edge_teams\Entity\TeamInterface;
use Drupal\apigee_edge_teams\Entity\TeamRoleInterface;
use Drupal\apigee_edge_teams\TeamMembershipManagerInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TeamMembersList extends ControllerBase {
private $teamMembershipManager;
protected $defaultRoles = [];
protected $moduleHandler;
public function __construct(TeamMembershipManagerInterface $team_membership_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler = NULL) {
if (!$module_handler) {
@trigger_error('Calling ' . __METHOD__ . ' without the $module_handler is deprecated in apigee_edge:8-x-1.19 and is required before apigee_edge:8.x-2.0. See https://github.com/apigee/apigee-edge-drupal/pull/518.', E_USER_DEPRECATED);
$module_handler = \Drupal::moduleHandler();
}
$this->teamMembershipManager = $team_membership_manager;
$this->entityTypeManager = $entity_type_manager;
if ($role = $this
->entityTypeManager()
->getStorage('team_role')
->load(TeamRoleInterface::TEAM_MEMBER_ROLE)) {
$this->defaultRoles = [
$role
->id() => $role
->label(),
];
}
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('apigee_edge_teams.team_membership_manager'), $container
->get('entity_type.manager'), $container
->get('module_handler'));
}
public function overview(TeamInterface $team) {
$entity_type = $this
->entityTypeManager()
->getDefinition('team');
$members = $this->teamMembershipManager
->getMembers($team
->id());
$users_by_mail = [];
$team_member_roles_by_mail = [];
if (!empty($members)) {
$user_storage = $this
->entityTypeManager()
->getStorage('user');
$uids = $user_storage
->getQuery()
->condition('mail', $members, 'IN')
->execute();
$users_by_mail = array_reduce($user_storage
->loadMultiple($uids), function ($carry, UserInterface $item) {
$carry[$item
->getEmail()] = $item;
return $carry;
}, []);
$team_member_role_storage = $this
->entityTypeManager()
->getStorage('team_member_role');
$team_member_roles_by_mail = array_reduce($team_member_role_storage
->loadByTeam($team), function ($carry, TeamMemberRoleInterface $developer_role) {
$carry[$developer_role
->getDeveloper()
->getEmail()] = $developer_role;
return $carry;
}, []);
}
$build['table'] = [
'#type' => 'table',
'#header' => [
'member' => $this
->t('Member'),
'roles' => $this
->t('Roles'),
'operations' => $this
->t('Operations'),
],
'#title' => $this
->t('@team Members', [
'@team' => $entity_type
->getSingularLabel(),
]),
'#rows' => [],
'#empty' => $this
->t('There are no members yet.'),
'#cache' => [
'contexts' => $team
->getCacheContexts(),
'tags' => $team
->getCacheTags(),
],
];
foreach ($members as $member) {
$build['table']['#rows'][$member] = $this
->buildRow($member, $users_by_mail, $team_member_roles_by_mail, $team);
}
if ($invitation_view = Views::getView('team_invitations')) {
$build['invitations'] = $invitation_view
->buildRenderable('team', [
'team' => $team
->id(),
]);
}
return $build;
}
protected function buildRow(string $member, array $users_by_mail, array $team_member_roles_by_mail, TeamInterface $team) : array {
$row = [];
$row['id'] = Html::getUniqueId($member);
if (array_key_exists($member, $users_by_mail)) {
$row['data']['member'] = $users_by_mail[$member]
->access('view') ? $users_by_mail[$member]
->toLink() : "{$users_by_mail[$member]->label()} ({$member})";
}
else {
$row['data']['member'] = $member;
}
if (array_key_exists($member, $team_member_roles_by_mail)) {
$roles = array_reduce($team_member_roles_by_mail[$member]
->getTeamRoles(), function ($carry, TeamRoleInterface $role) {
$carry[$role
->id()] = $role
->label();
return $carry;
}, $this->defaultRoles);
$row['data']['roles']['data'] = [
'#theme' => 'item_list',
'#items' => $roles,
'#cache' => [
'contexts' => $team_member_roles_by_mail[$member]
->getCacheContexts(),
'tags' => $team_member_roles_by_mail[$member]
->getCacheTags(),
],
];
}
else {
$row['data']['roles']['data'] = [
'#theme' => 'item_list',
'#items' => $this->defaultRoles,
];
}
$row['data']['operations']['data'] = $this
->buildOperations($member, $team);
return $row;
}
protected function buildOperations(string $member, TeamInterface $team) : array {
return [
'#type' => 'operations',
'#links' => $this
->getOperations($member, $team),
];
}
protected function getOperations(string $member, TeamInterface $team) {
$operations = [];
$url = Url::fromRoute('entity.team.member.edit', [
'team' => $team
->id(),
'developer' => $member,
], [
'query' => [
'destination' => $team
->toUrl('members')
->toString(),
],
]);
if ($url
->access()) {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => $url,
];
}
$url = Url::fromRoute('entity.team.member.remove', [
'team' => $team
->id(),
'developer' => $member,
], [
'query' => [
'destination' => $team
->toUrl('members')
->toString(),
],
]);
if ($url
->access()) {
$operations['remove'] = [
'title' => $this
->t('Remove'),
'url' => $url,
];
}
$this->moduleHandler
->alter('entity_operation', $operations, $team);
return $operations;
}
}