You are here

function formatted_number_widget_process in Formatted Number 6

Process an individual formatted_number element.

1 string reference to 'formatted_number_widget_process'
formatted_number_elements in ./formatted_number.module
Implementation of FAPI hook_elements().

File

./formatted_number.module, line 454
Defines CCK numeric types where thousands separator and decimal point are inherited from the Format Number API module.

Code

function formatted_number_widget_process($element, $edit, $form_state, $form) {
  $field_name = $element['#field_name'];
  $field = $form['#field_info'][$field_name];
  $field_key = $element['#columns'][0];
  $field_precision = isset($field['precision']) && (int) $field['precision'] > 0 ? (int) $field['precision'] : 12;
  $field_decimals = isset($field['decimals']) && (int) $field['decimals'] >= 0 ? (int) $field['decimals'] : 0;
  $field_min = isset($field['min']) ? parse_formatted_number($field['min']) : NULL;
  if (!is_numeric($field_min)) {
    $field_min = formatted_number_get_number_limit($field['type'], 'min', $field_precision, $field_decimals);
  }

  // Compute maxlength for the input textfield.
  $field_maxlength = $field_precision;
  $extra_length = 0;
  if ($field_decimals > 0) {
    $extra_length++;
  }
  if (isset($field_min) && $field_min < 0) {
    $extra_length++;
  }
  $thousands_sep = format_number_get_options('thousands_sep');
  if (!empty($thousands_sep)) {
    $extra_length += ceil($field_precision / 3);
  }

  // Format the default value.
  $default_value = isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : '';
  if (is_numeric($default_value)) {
    $default_value = format_number($default_value, $field_decimals);
  }
  $element[$field_key] = array(
    '#type' => 'textfield',
    '#default_value' => $default_value,
    // Need to 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_maxlength + $extra_length * 2 + 2,
    '#maxlength' => $field_maxlength + $extra_length,
    '#attributes' => array(
      'class' => 'formatted-number',
      'decimals' => $field_decimals,
    ),
    // The following values were set by the content module and need
    // to be passed down to the nested element.
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => $element['#required'],
    '#field_name' => $element['#field_name'],
    '#type_name' => $element['#type_name'],
    '#delta' => $element['#delta'],
    '#columns' => $element['#columns'],
  );

  // Add field prefix and/or suffix.
  if (!empty($field['prefix'])) {
    $prefixes = explode('|', $field['prefix']);
    $element[$field_key]['#field_prefix'] = array_pop($prefixes);
  }
  if (!empty($field['suffix'])) {
    $suffixes = explode('|', $field['suffix']);
    $element[$field_key]['#field_suffix'] = array_pop($suffixes);
  }

  // Make sure we don't wipe out element validation added elsewhere.
  if (empty($element['#element_validate'])) {
    $element['#element_validate'] = array();
  }
  $element['#element_validate'][] = 'formatted_number_widget_validate';

  // Used so that hook_field('validate') knows where to flag an error.
  $element['_error_element'] = array(
    '#type' => 'value',
    '#value' => implode('][', array_merge($element['#parents'], array(
      $field_key,
    ))),
  );
  return $element;
}