function country_property in Countries 8
Same name and namespace in other branches
- 7.2 countries.module \country_property()
Helper function to help standardise the display of the core properties.
Translatable strings are translated if possible and fallback strings are provided for the official name property.
Parameters
object $country: A country object.
string $property: The requested property.
array $options: A number if configurable options including:
- default: Default value to use if not set.
- sanitize: Boolean to indicate whether an HTML safe version is required.
- langcode: The required langauge for any translations.
Return value
string|int Integer cid or string representation of other properties.
19 calls to country_property()
- CountriesBaseSetupTest::assertCountryListed in tests/
countries.test - CountriesCRUDUIUnitTest::testISO2Conflict in tests/
countries.test - Special case where the ISO matches an existing ISO code.
- CountriesFunctionsUnitTest::testCountriesPropertyLookup in tests/
countries.test - This test that the country property lookup is working.
- countries_admin_form_validate in ./
countries.admin.inc - Validate country form submissions.
- countries_admin_overview in ./
countries.admin.inc - Menu callback; Displays a list of all countries.
File
- ./
countries.module, line 746 - Defines the field and entity information for countries.
Code
function country_property($country, $property = 'name', $options = array()) {
// Temporary wrapper for the API change between 7.x-2.1 and 7.x-2.2
// @todo Remove in 8.x and any 7.x-3.x branches.
$args = func_get_args();
if (!is_array($options)) {
$options = array(
'default' => $options,
);
}
if (isset($args[3])) {
$options['sanitize'] = $args[3];
}
$options += array(
'default' => NULL,
'sanitize' => TRUE,
'langcode' => NULL,
);
$t_options = array(
'langcode' => $options['langcode'],
);
$output = NULL;
switch ($property) {
case 'cid':
return $country->cid;
case 'enabled':
$t_options['context'] = 'countries';
return empty($country->enabled) ? t('Disabled', array(), $t_options) : t('Enabled', array(), $t_options);
case 'continent_code':
if (!empty($country->continent)) {
$output = $country->continent;
}
break;
case 'continent':
case 'continent_name':
$continents = countries_get_continents($t_options);
if (!empty($country->continent) && !empty($continents[$country->continent])) {
$output = $continents[$country->continent];
}
elseif (!isset($options['default'])) {
$t_options['context'] = 'countries';
$output = t('Unknown', array(), $t_options);
}
break;
case 'official_name':
// The function countries_t() returns an empty string if not set.
$output = countries_t($country, 'official_name', $options['langcode']);
if (!$output) {
if (isset($options['default'])) {
$output = $options['default'];
}
else {
$output = countries_t($country, 'name', $options['langcode']);
}
}
break;
case 'numcode':
// We do not need to sanitize this output.
if (!empty($country->numcode)) {
// @todo trace langcode into this function
return theme('countries_number', array(
'country' => $country,
'langcode' => $options['langcode'],
));
}
break;
case 'name':
// The function countries_t() returns an empty string if not set.
$output = countries_t($country, 'name', $options['langcode']);
if (!$output && isset($options['default'])) {
$output = $options['default'];
}
break;
default:
if (!empty($country->{$property})) {
$output = $country->{$property};
}
}
if (!isset($output)) {
$output = isset($options['default']) ? $options['default'] : '';
}
return $options['sanitize'] ? check_plain($output) : $output;
}