Skip to content

Commit

Permalink
Replace autocomplete with textfield on "add team member" form. (#450)
Browse files Browse the repository at this point in the history
* Replace autocomplete with textfield on "add team member" form.

* Replace autocomplete with textfield on "add team member" form - fix cdode sniffer validations.
  • Loading branch information
arlina-espinoza authored Jul 22, 2020
1 parent 9e27499 commit 7649de8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
69 changes: 50 additions & 19 deletions modules/apigee_edge_teams/src/Form/AddTeamMembersForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,11 @@ public function buildForm(array $form, FormStateInterface $form_state, TeamInter

$form['developers'] = [
'#title' => $this->t('Developers'),
'#description' => $this->t('Enter the email of one or more developers to add them to the @team.', [
'#description' => $this->t('Enter the email of one or more developers to add them to the @team, separated by comma.', [
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
]),
'#type' => 'entity_autocomplete',
'#target_type' => 'user',
'#tags' => TRUE,
'#type' => 'textfield',
'#required' => TRUE,
'#selection_handler' => 'apigee_edge_teams:team_members',
'#selection_settings' => [
'match_operator' => 'STARTS_WITH',
'filter' => ['team' => $this->team->id()],
],
];

$form['team_roles'] = [
Expand Down Expand Up @@ -143,25 +136,63 @@ public function buildForm(array $form, FormStateInterface $form_state, TeamInter
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.
*/
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 submitForm(array &$form, FormStateInterface $form_state) {
$logger = $this->logger('apigee_edge_teams');
// Collect user ids from submitted values.
$uids = array_map(function (array $item) {
return $item['target_id'];
}, $form_state->getValue('developers', []));

// Collect user accounts from submitted values.
list($developerAccounts, $notFound) = $this->getAccountsFromEmails($form_state->getValue('developers', ''));

if ($notFound) {
$this->messenger()->addWarning($this->t("Could not add developers to the @team because they don't yet have an account: @devs", [
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
'@devs' => implode(', ', $notFound),
]));
}

if (empty($developerAccounts)) {
return;
}

// Collect email addresses.
/** @var array $developer_emails */
$developer_emails = array_reduce($this->userStorage->loadMultiple($uids), function ($carry, UserInterface $item) {
$developer_emails = array_reduce($developerAccounts, function ($carry, UserInterface $item) {
$carry[$item->id()] = $item->getEmail();
return $carry;
}, []);

$context = [
'@developers' => implode('', $developer_emails),
'@developers' => implode(', ', $developer_emails),
'@team' => mb_strtolower($this->team->getEntityType()->getSingularLabel()),
'%team_id' => $this->team->id(),
];
Expand All @@ -177,16 +208,16 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
// multiple developers selected therefore we should not display that to
// the user.
$this->messenger()->addError($this->formatPlural(count($developer_emails),
$this->t('Failed to add developer to the @team.', $context),
$this->t('Failed to add developers to the @team.', $context
$this->t('Failed to add developer to the @team: @developers', $context),
$this->t('Failed to add developers to the @team: @developers', $context
)));
$logger->error('Failed to add developers to %team_id team. Developers: @developers. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
}

if ($success) {
$this->messenger()->addStatus($this->formatPlural(count($developer_emails),
$this->t('Developer successfully added to the @team.', $context),
$this->t('Developers successfully added to the @team.', $context
$this->t('Developer successfully added to the @team: @developers', $context),
$this->t('Developers successfully added to the @team: @developers', $context
)));
$form_state->setRedirectUrl($this->team->toUrl('members'));

Expand Down
5 changes: 4 additions & 1 deletion modules/apigee_edge_teams/tests/src/Functional/UiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,14 @@ protected function teamsWorkflowTest() {
$this->clickLink('Members');
$this->assertSession()->pageTextContains($this->account->getAccountName());
$this->clickLink('Add members');
$anotherEmail = $this->randomMachineName(10) . '@example.com';
$this->submitForm([
'developers' => "{$this->otherAccount->getEmail()} ({$this->otherAccount->id()})",
'developers' => "{$this->otherAccount->getEmail()}, $anotherEmail",
], 'Add members');
$this->assertSession()->pageTextContains($this->account->getAccountName());
$this->assertSession()->pageTextContains($this->otherAccount->getAccountName());
$this->assertSession()->pageTextContains('successfully added to the team: ' . $this->otherAccount->getEmail());
$this->assertSession()->pageTextContains('Could not add developers to the team because they don\'t yet have an account: ' . $anotherEmail);

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

0 comments on commit 7649de8

Please sign in to comment.