Skip to content

Commit 7649de8

Browse files
Replace autocomplete with textfield on "add team member" form. (#450)
* Replace autocomplete with textfield on "add team member" form. * Replace autocomplete with textfield on "add team member" form - fix cdode sniffer validations.
1 parent 9e27499 commit 7649de8

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

modules/apigee_edge_teams/src/Form/AddTeamMembersForm.php

+50-19
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,11 @@ public function buildForm(array $form, FormStateInterface $form_state, TeamInter
9090

9191
$form['developers'] = [
9292
'#title' => $this->t('Developers'),
93-
'#description' => $this->t('Enter the email of one or more developers to add them to the @team.', [
93+
'#description' => $this->t('Enter the email of one or more developers to add them to the @team, separated by comma.', [
9494
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
9595
]),
96-
'#type' => 'entity_autocomplete',
97-
'#target_type' => 'user',
98-
'#tags' => TRUE,
96+
'#type' => 'textfield',
9997
'#required' => TRUE,
100-
'#selection_handler' => 'apigee_edge_teams:team_members',
101-
'#selection_settings' => [
102-
'match_operator' => 'STARTS_WITH',
103-
'filter' => ['team' => $this->team->id()],
104-
],
10598
];
10699

107100
$form['team_roles'] = [
@@ -143,25 +136,63 @@ public function buildForm(array $form, FormStateInterface $form_state, TeamInter
143136
return $form;
144137
}
145138

139+
/**
140+
* Return an array of user UIDs given a list of emails.
141+
*
142+
* @param string $emails
143+
* The emails, comma separated.
144+
*
145+
* @return array
146+
* An array containing a first array of user accounts, and a second array of
147+
* emails that have no account on the system.
148+
*/
149+
protected function getAccountsFromEmails(string $emails): array {
150+
$developerEmails = [];
151+
$notFound = [];
152+
153+
$emails = array_map('trim', explode(',', $emails));
154+
155+
foreach ($emails as $email) {
156+
if ($account = user_load_by_mail($email)) {
157+
$developerEmails[$email] = $account;
158+
}
159+
else {
160+
$notFound[] = $email;
161+
}
162+
}
163+
164+
return [$developerEmails, $notFound];
165+
}
166+
146167
/**
147168
* {@inheritdoc}
148169
*/
149170
public function submitForm(array &$form, FormStateInterface $form_state) {
150171
$logger = $this->logger('apigee_edge_teams');
151-
// Collect user ids from submitted values.
152-
$uids = array_map(function (array $item) {
153-
return $item['target_id'];
154-
}, $form_state->getValue('developers', []));
172+
173+
// Collect user accounts from submitted values.
174+
list($developerAccounts, $notFound) = $this->getAccountsFromEmails($form_state->getValue('developers', ''));
175+
176+
if ($notFound) {
177+
$this->messenger()->addWarning($this->t("Could not add developers to the @team because they don't yet have an account: @devs", [
178+
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
179+
'@devs' => implode(', ', $notFound),
180+
]));
181+
}
182+
183+
if (empty($developerAccounts)) {
184+
return;
185+
}
155186

156187
// Collect email addresses.
157188
/** @var array $developer_emails */
158-
$developer_emails = array_reduce($this->userStorage->loadMultiple($uids), function ($carry, UserInterface $item) {
189+
$developer_emails = array_reduce($developerAccounts, function ($carry, UserInterface $item) {
159190
$carry[$item->id()] = $item->getEmail();
160191
return $carry;
161192
}, []);
162193

163194
$context = [
164-
'@developers' => implode('', $developer_emails),
195+
'@developers' => implode(', ', $developer_emails),
165196
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
166197
'%team_id' => $this->team->id(),
167198
];
@@ -177,16 +208,16 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
177208
// multiple developers selected therefore we should not display that to
178209
// the user.
179210
$this->messenger()->addError($this->formatPlural(count($developer_emails),
180-
$this->t('Failed to add developer to the @team.', $context),
181-
$this->t('Failed to add developers to the @team.', $context
211+
$this->t('Failed to add developer to the @team: @developers', $context),
212+
$this->t('Failed to add developers to the @team: @developers', $context
182213
)));
183214
$logger->error('Failed to add developers to %team_id team. Developers: @developers. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
184215
}
185216

186217
if ($success) {
187218
$this->messenger()->addStatus($this->formatPlural(count($developer_emails),
188-
$this->t('Developer successfully added to the @team.', $context),
189-
$this->t('Developers successfully added to the @team.', $context
219+
$this->t('Developer successfully added to the @team: @developers', $context),
220+
$this->t('Developers successfully added to the @team: @developers', $context
190221
)));
191222
$form_state->setRedirectUrl($this->team->toUrl('members'));
192223

modules/apigee_edge_teams/tests/src/Functional/UiTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,14 @@ protected function teamsWorkflowTest() {
195195
$this->clickLink('Members');
196196
$this->assertSession()->pageTextContains($this->account->getAccountName());
197197
$this->clickLink('Add members');
198+
$anotherEmail = $this->randomMachineName(10) . '@example.com';
198199
$this->submitForm([
199-
'developers' => "{$this->otherAccount->getEmail()} ({$this->otherAccount->id()})",
200+
'developers' => "{$this->otherAccount->getEmail()}, $anotherEmail",
200201
], 'Add members');
201202
$this->assertSession()->pageTextContains($this->account->getAccountName());
202203
$this->assertSession()->pageTextContains($this->otherAccount->getAccountName());
204+
$this->assertSession()->pageTextContains('successfully added to the team: ' . $this->otherAccount->getEmail());
205+
$this->assertSession()->pageTextContains('Could not add developers to the team because they don\'t yet have an account: ' . $anotherEmail);
203206

204207
// Team members have access to every team app and membership operations.
205208
$this->drupalPostForm(Url::fromRoute('apigee_edge_teams.settings.team.permissions'), [

0 commit comments

Comments
 (0)