1 number.module number_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element)

Implements hook_field_widget_form().

File

core/modules/field/modules/number/number.module, line 338
Defines numeric field types.

Code

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

  $element += array(
    '#type' => 'number',
    '#default_value' => $value,
    '#max' => 1000000,
  );

  // Set the step for floating point and decimal numbers.
  switch ($field['type']) {
    case 'number_decimal':
      // Avoid small float as base in function pow() for better results
      // regarding floating point precision.
      $scale = (int) $field['settings']['scale'];
      $element['#step'] = 1 / pow(10, $scale);
      break;

    case 'number_float':
      $element['#step'] = 'any';
      break;
  }

  // Set minimum and maximum.
  if (is_numeric($instance['settings']['min'])) {
    $element['#min'] = $instance['settings']['min'];
  }
  if (is_numeric($instance['settings']['max'])) {
    $element['#max'] = $instance['settings']['max'];
  }

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

  return array('value' => $element);
}