@@ -158,7 +158,7 @@ function dosomething_northstar_update_user($user, $payload) {
158158
159159 // Add to request log if enabled.
160160 dosomething_northstar_log_request('update_user', $user, $payload, $response);
161-
161+
162162 if ($response->code === '200' && module_exists('stathat')) {
163163 stathat_send_ez_count('drupal - Northstar - user updated - count', 1);
164164 }
@@ -205,12 +205,12 @@ function dosomething_northstar_get_northstar_user($drupal_id) {
205205}
206206
207207/**
208- * Build a user JSON object in the format that Northstar expects .
208+ * Transform Drupal user fields into the appropriate schema for Northstar .
209209 *
210210 * @param $user - Drupal user object
211211 * @return array
212212 */
213- function dosomething_northstar_build_ns_user ($user, $form_state ) {
213+ function dosomething_northstar_transform_user ($user) {
214214 // Optional fields
215215 $optional = [
216216 'mobile' => 'field_mobile',
@@ -229,42 +229,61 @@ function dosomething_northstar_build_ns_user($user, $form_state) {
229229 'addr_zip' => 'postal_code',
230230 ];
231231
232- $ns_user = [
232+ $northstar_user = [
233233 'email' => $user->mail,
234234 'drupal_id' => $user->uid,
235235 'language' => $user->language,
236236 ];
237237
238238 // Set values in ns_user if they are set.
239239 foreach ($optional as $ns_key => $drupal_key) {
240- $field = $user->$drupal_key;
240+ $field = $user->$drupal_key;
241241 if (isset($field[LANGUAGE_NONE][0]['value'])) {
242- $ns_user [$ns_key] = $field[LANGUAGE_NONE][0]['value'];
242+ $northstar_user [$ns_key] = $field[LANGUAGE_NONE][0]['value'];
243243 }
244244 }
245245 foreach ($address as $ns_key => $drupal_key) {
246246 $field = $user->field_address[LANGUAGE_NONE][0];
247247
248248 if ($drupal_key == 'country') {
249- $ns_user [$ns_key] = $field[$drupal_key];
249+ $northstar_user [$ns_key] = $field[$drupal_key];
250250 } else if (isset($field[$drupal_key]['value'])) {
251- $ns_user [$ns_key] = $field[$drupal_key]['value'];
251+ $northstar_user [$ns_key] = $field[$drupal_key]['value'];
252252 }
253253 }
254254
255- // Don't send blank passwords from the user update screen.
256- if (!empty($form_state['values']['pass'])) {
257- $ns_user['password'] = $form_state['values']['pass'];
255+ // If user has a "1234565555@mobile" placeholder email, don't send
256+ // that to Northstar (since it will cause a validation error and Northstar
257+ // doesn't require every account to have an email like Drupal does).
258+ if(preg_match('/^[0-9]+@mobile$/', $northstar_user['email'])) {
259+ unset($northstar_user['email']);
258260 }
259261
260262 // Set the "source" for this user to Phoenix if they weren't
261263 // programmatically created through the API.
262- if(empty($ns_user['source'])) {
263- $ns_user['source'] = 'phoenix';
264+ if(empty($northstar_user['source'])) {
265+ $northstar_user['source'] = 'phoenix';
266+ }
267+
268+ return $northstar_user;
269+ }
270+
271+ /**
272+ * Build a Northstar user from a Drupal form submission.
273+ *
274+ * @param $user - Drupal user object
275+ * @param $form_state - Form fields from registration/profile form.
276+ * @return array
277+ */
278+ function dosomething_northstar_build_ns_user($user, $form_state) {
279+ $northstar_user = dosomething_northstar_transform_user($user);
280+
281+ // Don't send blank passwords from the user update screen.
282+ if (!empty($form_state['values']['pass'])) {
283+ $northstar_user['password'] = $form_state['values']['pass'];
264284 }
265285
266- // Return fully built northstar user object.
267- return $ns_user;
286+ return $northstar_user;
268287}
269288
270289/**
0 commit comments