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 392 - 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,
);
// Set default field values based on the number type.
switch ($field['type']) {
case 'number_decimal':
// Calculate the minimum and maximum value of the decimal field based on
// precision and scale settings.
$number_value = intval(('1e+' . ($field['settings']['precision'] - $field['settings']['scale'])) - 1);
$decimal_value = intval(('1e+' . $field['settings']['scale']) - 1);
$element['#min'] = (float) ('-' . $number_value . '.' . $decimal_value);
$element['#max'] = (float) ($number_value . '.' . $decimal_value);
// 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['#min'] = -3.402823466E+38;
$element['#max'] = 3.402823466E+38;
$element['#step'] = 'any';
break;
case 'number_integer':
$element['#min'] = '-2147483647';
$element['#max'] = '2147483647';
}
// Override the default minimum and maximum settings if configured.
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);
}