From 7649de826de559fa6d3ae07cef00af61e13fcde1 Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Rhoton Date: Wed, 22 Jul 2020 00:02:30 -0700 Subject: [PATCH] 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. --- .../src/Form/AddTeamMembersForm.php | 69 ++++++++++++++----- .../tests/src/Functional/UiTest.php | 5 +- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/modules/apigee_edge_teams/src/Form/AddTeamMembersForm.php b/modules/apigee_edge_teams/src/Form/AddTeamMembersForm.php index 83b76411..d60a935c 100644 --- a/modules/apigee_edge_teams/src/Form/AddTeamMembersForm.php +++ b/modules/apigee_edge_teams/src/Form/AddTeamMembersForm.php @@ -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'] = [ @@ -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(), ]; @@ -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).
@backtrace_string
', $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')); diff --git a/modules/apigee_edge_teams/tests/src/Functional/UiTest.php b/modules/apigee_edge_teams/tests/src/Functional/UiTest.php index a868f997..5f20ffd1 100644 --- a/modules/apigee_edge_teams/tests/src/Functional/UiTest.php +++ b/modules/apigee_edge_teams/tests/src/Functional/UiTest.php @@ -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'), [