| 1 |
<?php
|
| 2 |
// $Id: country_code.inc,v 1.20 2008/10/30 17:49:12 nedjo Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Include file to be included in hook_boot(). Because the functions here may
|
| 7 |
* be run before Drupal is fully bootstrapped, care must be taken not to call
|
| 8 |
* module functions.
|
| 9 |
*
|
| 10 |
* TODO: hook_requirements() for checking for other modules implementing url
|
| 11 |
* rewriting functions.
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Rewrite outbound paths to have a country code prefix.
|
| 16 |
*/
|
| 17 |
function custom_url_rewrite_outbound(&$path, &$options, $original_path, $reset = FALSE) {
|
| 18 |
static $module_exists, $country_codes, $country_code_prefix;
|
| 19 |
|
| 20 |
if ($reset || !isset($module_exists)) {
|
| 21 |
$module_exists = module_exists('country_code');
|
| 22 |
$country_codes = array_keys(country_code_countries());
|
| 23 |
$country_code_prefix = _country_code_prefix();
|
| 24 |
}
|
| 25 |
// Only modify relative (insite) URLs.
|
| 26 |
if ($module_exists && !$options['external']) {
|
| 27 |
// Don't modify links that already have a country prefix.
|
| 28 |
$args = explode('/', $path);
|
| 29 |
$prefix = array_shift($args);
|
| 30 |
// If there's a global prefix, remove it.
|
| 31 |
if ($prefix == COUNTRY_CODE_GLOBAL) {
|
| 32 |
$path = implode('/', $args);
|
| 33 |
}
|
| 34 |
elseif (!in_array($prefix, $country_codes)) {
|
| 35 |
$options['prefix'] = $country_code_prefix;
|
| 36 |
}
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
/**
|
| 41 |
* Rewrite inbound paths to remove a country code prefix.
|
| 42 |
*/
|
| 43 |
function custom_url_rewrite_inbound(&$result, $path, $path_language, $reset = FALSE) {
|
| 44 |
static $module_exists, $prefix;
|
| 45 |
|
| 46 |
if ($reset || !isset($module_exists)) {
|
| 47 |
$module_exists = module_exists('country_code');
|
| 48 |
$prefix = _country_code_prefix();
|
| 49 |
}
|
| 50 |
// Only modify paths having the prefix added by custom_url_rewrite_outbound().
|
| 51 |
if ($module_exists && substr($path, 0, 3) == $prefix) {
|
| 52 |
$result = substr($path, 3);
|
| 53 |
}
|
| 54 |
}
|