function money_get_widget_currencies in Money field 7
Same name and namespace in other branches
- 6 money.module \money_get_widget_currencies()
Build currency options for the given field/widget.
1 call to money_get_widget_currencies()
- money_field_widget_form in ./
money.module - Implements hook_field_widget_form().
File
- ./
money.module, line 358 - This module defines the Money field.
Code
function money_get_widget_currencies($instance, $element) {
$widget = $instance['widget'];
$settings = $widget['settings'];
// Currently implemented modes: code, name. See money_field_widget_settings_form().
$mode = $settings['currency_select_mode'];
// Prepare the array of allowed currencies.
if (isset($settings['currencies']['allowed_currencies']) && is_array($settings['currencies']['allowed_currencies'])) {
// Obtain the list of allowed currencies. Note that this array is in the form of 'code' => boolean.
$allowed_currencies = array_filter($settings['currencies']['allowed_currencies']);
}
else {
// Initialize array when the list has not been already set in field settings.
$allowed_currencies = array();
}
// When no currency has been specified in widget settings we allow them all.
if (empty($allowed_currencies)) {
// Note that this array is built in the form of 'code' => 'name'.
$allowed_currencies = currency_options();
}
else {
// One or more currencies have been specified in widget settings.
if ($mode == 'name') {
// Build the array in the form of 'code' => 'name' extracting the
// allowed currencies from the array returned from currency_api.
$allowed_currencies = array_intersect_key(currency_options(), $allowed_currencies);
}
}
// If the requested mode is 'code', then we need to transform the array
// so that item keys are also used for values.
if ($mode == 'code') {
$allowed_currencies = array_keys($allowed_currencies);
$allowed_currencies = array_combine($allowed_currencies, $allowed_currencies);
}
// When field is not required, an additional empty currency is pushed on top of the resulting list.
if (!$element['#required']) {
$allowed_currencies = array(
'' => $mode == 'code' ? '---' : t('-- Select currency --'),
) + $allowed_currencies;
}
return $allowed_currencies;
}