@@ -90,18 +90,11 @@ public function buildForm(array $form, FormStateInterface $form_state, TeamInter
90
90
91
91
$ form ['developers ' ] = [
92
92
'#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 . ' , [
94
94
'@team ' => mb_strtolower ($ this ->team ->getEntityType ()->getSingularLabel ()),
95
95
]),
96
- '#type ' => 'entity_autocomplete ' ,
97
- '#target_type ' => 'user ' ,
98
- '#tags ' => TRUE ,
96
+ '#type ' => 'textfield ' ,
99
97
'#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
- ],
105
98
];
106
99
107
100
$ form ['team_roles ' ] = [
@@ -143,25 +136,63 @@ public function buildForm(array $form, FormStateInterface $form_state, TeamInter
143
136
return $ form ;
144
137
}
145
138
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
+
146
167
/**
147
168
* {@inheritdoc}
148
169
*/
149
170
public function submitForm (array &$ form , FormStateInterface $ form_state ) {
150
171
$ 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
+ }
155
186
156
187
// Collect email addresses.
157
188
/** @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 ) {
159
190
$ carry [$ item ->id ()] = $ item ->getEmail ();
160
191
return $ carry ;
161
192
}, []);
162
193
163
194
$ context = [
164
- '@developers ' => implode ('' , $ developer_emails ),
195
+ '@developers ' => implode (', ' , $ developer_emails ),
165
196
'@team ' => mb_strtolower ($ this ->team ->getEntityType ()->getSingularLabel ()),
166
197
'%team_id ' => $ this ->team ->id (),
167
198
];
@@ -177,16 +208,16 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
177
208
// multiple developers selected therefore we should not display that to
178
209
// the user.
179
210
$ 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
182
213
)));
183
214
$ logger ->error ('Failed to add developers to %team_id team. Developers: @developers. @message %function (line %line of %file). <pre>@backtrace_string</pre> ' , $ context );
184
215
}
185
216
186
217
if ($ success ) {
187
218
$ 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
190
221
)));
191
222
$ form_state ->setRedirectUrl ($ this ->team ->toUrl ('members ' ));
192
223
0 commit comments