Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Commit 1c2f555

Browse files
author
Joe Kent
committed
Merge pull request #5458 from deadlybutter/5349
Redirects users on homepage to proper path prefix
2 parents 2cc46e8 + 479269d commit 1c2f555

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

lib/modules/dosomething/dosomething_global/dosomething_global.module

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,36 @@ define('DOSOMETHING_GLOBAL_DEFAULT_LANG_CODE', 'en-global');
1616
function dosomething_global_init() {
1717
global $user;
1818

19+
// Redirect people hitting the homepage or explore campaigns to their local version
20+
$path = request_path();
21+
if ((drupal_is_front_page() && is_numeric(arg(1))) || $path == 'campaigns') {
22+
// Cannot use arg() as it doesn't return path prefixes
23+
$args = explode('/', $path);
24+
25+
// Only apply logic to Global homepage
26+
if (drupal_is_front_page()) {
27+
if (!empty($path) && $args[0] != 'node') {
28+
return;
29+
}
30+
}
31+
32+
$country_code = dosomething_settings_get_geo_country_code();
33+
$language = dosomething_global_convert_country_to_language($country_code);
34+
// No matching language, serve the global page
35+
if ($language == NULL) {
36+
return;
37+
}
38+
39+
// Get users local path prefix and verify we support it
40+
$path_prefix = dosomething_global_get_prefix_for_language($language);
41+
if ($path_prefix == NULL) {
42+
return;
43+
}
44+
45+
// Redirect user
46+
drupal_goto($path_prefix . '/' . current_path());
47+
}
48+
1949
// Verify we're dealing with a node edit with no translation specification in the URL
2050
if (arg(0) == "node" && is_numeric(arg(1)) && arg(2) == "edit" && null == arg(3)) {
2151
// Load the page node and user
@@ -317,6 +347,20 @@ function dosomething_global_convert_country_to_language($country) {
317347
return NULL;
318348
}
319349

350+
/**
351+
* Converts the given language into its assigned URL prefix.
352+
*
353+
* @param string language
354+
* The language to get the prefix for (eg: 'en', 'mx')
355+
*
356+
* @return
357+
* The URL prefix mapped to this language
358+
*/
359+
function dosomething_global_get_prefix_for_language($language) {
360+
$languages = language_list();
361+
return $languages[$language]->prefix;
362+
}
363+
320364
/**
321365
* Get the appropriate language for the application.
322366
*

0 commit comments

Comments
 (0)