public function UcAddressesUcCountryFieldHandler::mapValue in Ubercart Addresses 7
Overrides UcAddressesFieldHandler::mapValue().
The country field has some extra mapping targets.
Overrides UcAddressesFieldHandler::mapValue
File
- handlers/
ubercart.handlers.inc, line 382 - Field handlers for Ubercart core address fields: first_name, last_name, company, etc.
Class
- UcAddressesUcCountryFieldHandler
- Class for the Ubercart country field.
Code
public function mapValue($value, $format = '') {
if (strpos($format, 'country_name:') === 0) {
// Country is specified in native or current language.
$explode = explode(':', $format);
$langcode = $explode[1];
if ($langcode == 'current') {
// Country is expected to be specified in the current language.
global $language;
$langcode = $language->language;
}
// Look through complete country list and find one that matches.
$countries = $this
->getAllCountries();
foreach ($countries as $country) {
$translated_country = t($country->country_name, array(), array(
'langcode' => $langcode,
));
if ($value == $translated_country) {
// Country in native language found!
return parent::mapValue($country->country_id, $format);
}
}
// No country found. Report error.
$message = t('Country %name not found.', array(
'%name' => $value,
));
if (user_access('administer store')) {
$message .= ' ' . t('Check if the country is spelled correctly and if it is installed in the store.');
}
else {
$message .= ' ' . t('Check if the country is spelled correctly or contact the site administrator.');
}
throw new UcAddressesException($message);
}
switch ($format) {
case 'country_code2':
case 'country_code2_if':
$format = 'country_iso_code_2';
break;
case 'country_code3':
case 'country_code3_if':
$format = 'country_iso_code_3';
break;
}
switch ($format) {
case 'country_name':
case 'country_iso_code_2':
case 'country_iso_code_3':
// Lookup country data.
$country_id = db_select('uc_countries', 'uc_countries')
->condition($format, $value)
->fields('uc_countries', array(
'country_id',
))
->execute()
->fetchField();
if ($country_id) {
$value = $country_id;
}
else {
$message = t('Country @name not found.', array(
'@name' => $value,
));
if (user_access('administer store')) {
$message .= ' ' . t('Check if the country is spelled correctly and if it is enabled in the store.');
}
else {
$message .= ' ' . t('Check if the country is spelled correctly or contact the site administrator.');
}
throw new UcAddressesException($message);
}
break;
}
parent::mapValue($value, $format);
}