You are here

function money_field_widget_form in Money field 7

Implements hook_field_widget_form().

File

./money.module, line 306
This module defines the Money field.

Code

function money_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $value = isset($items[$delta]['amount']) ? $items[$delta]['amount'] : '';

  // Dependency required by CurrencyLocalePattern class.
  ctools_include('export');
  $decimal_separator = CurrencyLocalePattern::loadFromEnv()->symbol_decimal_separator;

  // Substitute the decimal separator.
  $value = strtr($value, '.', $decimal_separator);
  $element += array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
  );
  $element['amount'] = array(
    '#type' => 'textfield',
    '#default_value' => $value,
    // Allow a slightly larger size than the field length to allow for some
    // configurations where all characters won't fit in input field.
    '#size' => $field['settings']['precision'] + 4,
    // Allow two extra characters for signed values and decimal separator.
    '#maxlength' => $field['settings']['precision'] + 2,
    '#attributes' => array(
      'class' => array(
        'formatted-number',
      ),
      'decimals' => $field['settings']['scale'],
    ),
    '#attached' => array(
      'css' => array(),
      'js' => array(
        // Removed dependency to format_number module.
        // Removed dependency to formatted_number module.
        array(
          'data' => array(),
          // Removed dependency to format_number module.
          'type' => 'setting',
        ),
      ),
    ),
  );
  $element['currency'] = array(
    '#type' => 'select',
    '#default_value' => isset($items[$delta]['currency']) ? $items[$delta]['currency'] : array(),
    '#options' => money_get_widget_currencies($instance, $element),
  );
  $element['#element_validate'][] = 'money_field_widget_validate';
  return $element;
}