function format_number_numericfield_process in Format Number API 7
Same name and namespace in other branches
- 6 format_number.module \format_number_numericfield_process()
Process an individual numeric form element.
Parameters
$element: The form element being processed.
$edit: The incoming POST data to populate the form element.
$form_state: A keyed array containing the current state of the form.
$form: An associative array containing the structure of the form.
1 string reference to 'format_number_numericfield_process'
- format_number_element_info in ./
format_number.module - Implements hook_element_info().
File
- ./
format_number.module, line 421 - This module provides a method to configure number formats (site default and user defined) with configurable decimal point and thousand separators. It also exposes several functions that can be used by other contributed or custom modules to display…
Code
function format_number_numericfield_process($element, &$form_state) {
$element_precision = isset($element['#precision']) && (int) $element['#precision'] > 0 ? (int) $element['#precision'] : 12;
$element_decimals = isset($element['#decimals']) && (int) $element['#decimals'] >= 0 ? (int) $element['#decimals'] : 0;
$element_minimum = isset($element['#minimum']) ? parse_formatted_number($element['#minimum']) : NULL;
if (!is_numeric($element_minimum)) {
$element_minimum = format_number_compute_boundary('lower', $element_precision, $element_decimals);
}
// Compute size and maxlength for the input element, but still allow the
// user specify these values in the form definition.
if (empty($element['#size']) || empty($element['#maxlength'])) {
$element_maxlength = $element_precision;
if ($element_decimals > 0) {
$element_maxlength++;
}
if (isset($element_minimum) && $element_minimum < 0) {
$element_maxlength++;
}
$thousands_sep = format_number_get_options('thousands_sep');
if (!empty($thousands_sep)) {
$element_maxlength += ceil(($element_precision - $element_decimals) / 3) - 1;
}
if (empty($element['#size'])) {
$element['#size'] = $element_maxlength + 1;
}
if (empty($element['#maxlength'])) {
$element['#maxlength'] = $element_maxlength;
}
}
// Format the element value. A valid PHP number is expected. It may come from
// user defined form or from element value callback. In any case, our element
// validation callback will generate a valid PHP number, or flag an error. Our
// goal now is generate a number formatted with site or user defined options,
// if value is a valid PHP number.
$value = !empty($element['#value']) && is_string($element['#value']) ? $element['#value'] : '';
if (is_numeric($value)) {
$value = format_number($value, $element_decimals);
}
$element['#value'] = $value;
// Pass decimal places to the client-side javascript using HTML attribute.
if (isset($element['#attributes']) && is_array($element['#attributes'])) {
$element['#attributes']['decimals'] = $element_decimals;
}
else {
$element['#attributes'] = array(
'decimals' => $element_decimals,
);
}
return $element;
}