You are here

function currency_form_currency_amount_process in Currency 7.2

Implements form process callback for a currency_amount element.

1 string reference to 'currency_form_currency_amount_process'
currency_element_info in currency/currency.module
Implements hook_element_info().

File

currency/currency.module, line 221
Provides currency information and allows users to add custom currencies.

Code

function currency_form_currency_amount_process(array $element, array &$form_state, array &$form) {

  // Validate element configuration.
  if ($element['#minimum_amount'] !== FALSE && !is_numeric($element['#minimum_amount'])) {
    throw new Exception(t('The minimum amount must be a decimal number.'));
  }
  if ($element['#maximum_amount'] !== FALSE && !is_numeric($element['#maximum_amount'])) {
    throw new Exception(t('The maximum amount must be a decimal number.'));
  }
  if ($element['#currency_code'] && isset($element['#default_value']['currency_code']) && $element['#default_value']['currency_code'] != $element['#limit_currency_codes']) {
    throw new \InvalidArgumentException(sprintf('The default currency %s is different from the only allowed currency %s.', $element['#default_value']['currency_code'], $element['#currency_code']));
  }

  // Load the default currency.
  ctools_include('export');
  $currency = NULL;
  if (isset($element['#default_value']['currency_code'])) {
    $currency = currency_load($element['#default_value']['currency_code']);
  }
  if (!$currency && $element['#currency_code']) {
    $currency = currency_load($element['#currency_code']);
  }
  if (!$currency) {
    $currency = currency_load('XXX');
  }

  // Modify the element.
  $element['#tree'] = TRUE;
  $element['#theme_wrappers'][] = 'form_element';
  $element['#attached']['css'] = array(
    drupal_get_path('module', 'currency') . '/currency.css',
  );

  // Add the currency element.
  if (!$element['#currency_code']) {
    $element['currency_code'] = array(
      '#default_value' => $currency->ISO4217Code,
      '#type' => 'select',
      '#title' => t('Currency'),
      '#title_display' => 'invisible',
      '#options' => currency_options(),
      '#required' => $element['#required'],
    );
  }

  // Add the amount element.
  $description = NULL;
  if ($element['#minimum_amount'] !== FALSE) {
    $description = t('The minimum amount is !amount.', array(
      '!amount' => $currency
        ->format($element['#minimum_amount']),
    ));
  }
  $element['amount'] = array(
    '#default_value' => $element['#default_value']['amount'],
    '#type' => 'textfield',
    '#title' => t('Amount'),
    '#title_display' => 'invisible',
    '#description' => $description,
    '#prefix' => $element['#currency_code'] ? $currency->sign : NULL,
    '#required' => $element['#required'],
    '#size' => 9,
  );
  return $element;
}