Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ function dosomething_global_convert_language_to_country($language) {
}

/**
* Converts the given country into its assigned language
* Converts the given country into its assigned language.
*
* @param string country
* The country Code
*
* @return
* The language mapped to this country
* The language mapped to this country or NULL
*/
function dosomething_global_convert_country_to_language($country) {
$lang_map = variable_get('dosomething_global_language_map', '');
Expand All @@ -220,7 +220,8 @@ function dosomething_global_convert_country_to_language($country) {
return $lang_key;
}
}
return 'en';
// It is not the job of this function to determine a default language
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add comments for why we are returning null here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep- just to add it here first,

this function shouldn't be deciding what the correct default value should be, as the name implies its only purpose is to find the matching language for that country. In some cases, that country doesn't have a matching language and it's useful to have it return a default lang, in others, it might be more preferable to have it return NULL.

}

/*
Expand Down
64 changes: 53 additions & 11 deletions lib/modules/dosomething/dosomething_user/dosomething_user.module
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ function dosomething_user_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_register_form' && $_SERVER['REQUEST_URI'] != '/admin/people/create') {
$form['#action'] = '/user/register';
$form['#submit'][] = 'dosomething_user_new_user';
if (module_exists('dosomething_global')) {
$form['#submit'][] = 'dosomething_user_new_user_attributes';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_user_global_attributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
// Unsets relevant register form fields based on configuration variables.
_dosomething_user_register_display_fields($form);
// Add campaign data, if needed.
Expand Down Expand Up @@ -464,17 +467,6 @@ function dosomething_user_new_user($form, &$form_state) {
global $user;
$account = $user;

if (module_exists('dosomething_global')) {
// Adjust language based on location
$user_country_code = dosomething_settings_get_geo_country_code();
$language = dosomething_global_convert_country_to_language($user_country_code);
$system_languages = language_list();
if (!in_array($langauge, $system_languages)) {
$language = 'en-global';
}
user_save($account, ['language' => $language]);
}

// Should we sign this kid up for messages?
if (dosomething_user_is_under_thirteen()) {
return;
Expand Down Expand Up @@ -523,6 +515,56 @@ function dosomething_user_new_user($form, &$form_state) {
dosomething_mbp_request('user_register', $params);
}

function dosomething_user_new_user_attributes($form, &$form_state) {
dosomething_user_set_global_attributes();
}

/**
* Sets the user global attributes and sends them to Northstar.
*
* @param object $account
* The account to return value for. If NULL, uses global $user.
* @param string $country_code
* The country code where the user currently resides. If NULL, uses
* the headers in current request.
* @param string $language
* The new language to set on the user. If NULL, gets the matching
* language or defaults.
*/
function dosomething_user_set_global_attributes($user = NULL, $country_code = NULL, $language = NULL) {
if ($user == NULL) {
global $user;
$account = $user;
}
if ($country_code == NULL) {
$country_code = dosomething_settings_get_geo_country_code();
// If no Fastly headers set, use US EN
if ($country_code == NULL) {
$language = 'en';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$language = 'en'; and $language = 'en-global'; on ln 544. Are they two different languages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they are two different languages. en is just for US, en global is for all countries outside of the US we don't explicitly support

}
}
// If the language wasn't specified in the function call, determine the user language
if ($language == NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment block above explaining the logic that happens below?

$converted_language = dosomething_global_convert_country_to_language($country_code);
$language = 'en-global';

// If the country in the headers matched to a language in our strongarm
if ($converted_language != NULL) {

// Verify the language is installed on the system,
$system_languages = language_list();
foreach ($system_languages as $lang_key => $system_lang) {
if ($converted_language == $lang_key) {
$language = $converted_language;
break;
}
}
}
}
user_save($account, ['language' => $language]);
//TODO: (#5263) Send $language and $country_code to NorthStar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will also want to save user country to the user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this field anywhere, has it been created yet?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a part of field_address

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh gotcha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/**
* Checks that user is over 13.
*
Expand Down