You are here

function money_get_widget_currencies in Money field 6

Same name and namespace in other branches
  1. 7 money.module \money_get_widget_currencies()

Build currency options for the given field/widget.

1 call to money_get_widget_currencies()
money_widget_process in ./money.module
Process an individual Money CCK field element.

File

./money.module, line 336
This module defines the Money CCK field.

Code

function money_get_widget_currencies($field) {

  // Currently implemented modes: code, name. See money_widget_settings().
  $mode = $field['widget']['currency_select_mode'];

  // Prepare the array of allowed currencies.
  if (isset($field['widget']['allowed_currencies']) && is_array($field['widget']['allowed_currencies'])) {

    // Obtain the list of allowed currencies. Note that this array is in the form of 'code' => boolean.
    $allowed_currencies = array_filter($field['widget']['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_api_get_list();
  }
  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_api_get_list(), $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 (!$field['required']) {
    $allowed_currencies = array(
      '' => $mode == 'code' ? '---' : t('-- Select currency --'),
    ) + $allowed_currencies;
  }
  return $allowed_currencies;
}