-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathAddTeamMembersForm.php
252 lines (217 loc) · 8.34 KB
/
AddTeamMembersForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* Copyright 2018 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
namespace Drupal\apigee_edge_teams\Form;
use Drupal\apigee_edge_teams\Entity\TeamInterface;
use Drupal\apigee_edge_teams\Entity\TeamInvitationInterface;
use Drupal\apigee_edge_teams\Entity\TeamRoleInterface;
use Drupal\apigee_edge_teams\TeamMembershipManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Add team members form.
*/
class AddTeamMembersForm extends TeamMembersFormBase {
/**
* The team membership manager service.
*
* @var \Drupal\apigee_edge_teams\TeamMembershipManagerInterface
*/
protected $teamMembershipManager;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The team invitation storage.
*
* @var \Drupal\apigee_edge_teams\Entity\Storage\TeamInvitationStorageInterface
*/
protected $teamInvitationStorage;
/**
* AddTeamMemberForms constructor.
*
* @param \Drupal\apigee_edge_teams\TeamMembershipManagerInterface $team_membership_manager
* The team membership manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(TeamMembershipManagerInterface $team_membership_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type_manager);
$this->teamMembershipManager = $team_membership_manager;
$this->userStorage = $entity_type_manager->getStorage('user');
$this->teamInvitationStorage = $entity_type_manager->getStorage('team_invitation');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('apigee_edge_teams.team_membership_manager'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'apigee_edge_teams_add_team_member_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, TeamInterface $team = NULL) {
$this->team = $team;
$role_options = $this->getRoleOptions();
$form['developers'] = [
'#title' => $this->t('Developers'),
'#description' => $this->t('Enter the email of one or more developers to invite them to the @team, separated by comma.', [
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
]),
'#type' => 'textarea',
'#required' => TRUE,
];
$form['team_roles'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Roles'),
'#options' => $role_options,
'#multiple' => TRUE,
'#required' => FALSE,
];
// Special handling for the inevitable team member role.
$form['team_roles'][TeamRoleInterface::TEAM_MEMBER_ROLE] = [
'#default_value' => TRUE,
'#disabled' => TRUE,
];
$form['team_roles']['description'] = [
'#markup' => $this->t('Assign one or more roles to <em>all developers</em> that you selected in %team_label @team.', [
'%team_label' => $this->team->label(),
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
]),
];
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Invite members'),
'#button_type' => 'primary',
],
'cancel' => [
'#type' => 'link',
'#title' => $this->t('Cancel'),
'#attributes' => ['class' => ['button']],
'#url' => $this->team->toUrl('members'),
],
];
return $form;
}
/**
* Return an array of user UIDs given a list of emails.
*
* @param string $emails
* The emails, comma separated.
*
* @return array
* An array containing a first array of user accounts, and a second array of
* emails that have no account on the system.
*
* @deprecated in apigee_edge_teams:8.x-1.12 and is removed from apigee_edge_teams:2.x. No replacement.
*
* @see https://github.com/apigee/apigee-edge-drupal/pull/474
*/
protected function getAccountsFromEmails(string $emails): array {
$developerEmails = [];
$notFound = [];
$emails = array_map('trim', explode(',', $emails));
foreach ($emails as $email) {
if ($account = user_load_by_mail($email)) {
$developerEmails[$email] = $account;
}
else {
$notFound[] = $email;
}
}
return [$developerEmails, $notFound];
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$emails = array_map('trim', explode(',', $form_state->getValue('developers', '')));
$members = $this->teamMembershipManager->getMembers($this->team->id());
$already_members = array_unique(array_intersect($emails, $members));
// Validate existing members.
if (count($already_members)) {
$form_state->setErrorByName('developers', $this->formatPlural(count($already_members), 'The following developer is already a member of the @team: %developers.', 'The following developers are already members of the @team: %developers.', [
'%developers' => implode(', ', $already_members),
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
]));
}
// Validate pending invitations.
$invites = array_diff($emails, $members);
$has_invitation = [];
foreach ($invites as $invite) {
$pending_invitations = array_filter($this->teamInvitationStorage->loadByRecipient($invite, $this->team->id()), function (TeamInvitationInterface $team_invitation) {
return $team_invitation->isPending();
});
if (count($pending_invitations)) {
$has_invitation[] = $invite;
}
}
$has_invitation = array_unique($has_invitation);
if (count($has_invitation)) {
$form_state->setErrorByName('developers', $this->formatPlural(count($has_invitation), 'The following developer has already been invited to the @team: %developers.', 'The following developers have already been invited to the @team: %developers.', [
'%developers' => implode(', ', $has_invitation),
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
]));
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$emails = array_map('trim', explode(',', $form_state->getValue('developers', '')));
$selected_roles = $this->filterSelectedRoles($form_state->getValue('team_roles', []));
// Add default member role.
$selected_roles = [TeamRoleInterface::TEAM_MEMBER_ROLE => TeamRoleInterface::TEAM_MEMBER_ROLE] + $selected_roles;
// Create an invitation for each email.
foreach ($emails as $email) {
$this->teamInvitationStorage->create([
'team' => ['target_id' => $this->team->id()],
'team_roles' => array_values(array_map(function (string $role) {
return ['target_id' => $role];
}, $selected_roles)),
'recipient' => $email,
])->save();
}
$context = [
'@developers' => implode(', ', $emails),
'@team' => $this->team->label(),
'@team_label' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
];
$this->messenger()->addStatus($this->formatPlural(count($emails),
$this->t('The following developer has been invited to the @team @team_label: @developers.', $context),
$this->t('The following developers have been invited to the @team @team_label: @developers.', $context
)));
$form_state->setRedirectUrl($this->team->toUrl('members'));
}
}