function formatted_number_field_settings in Formatted Number 6
Implementation of hook_field_settings().
File
- ./
formatted_number.module, line 225 - Defines CCK numeric types where thousands separator and decimal point are inherited from the Format Number API module.
Code
function formatted_number_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array();
$form['append']['prefix'] = array(
'#type' => 'textfield',
'#title' => t('Prefix'),
'#size' => 40,
'#maxlength' => 60,
'#default_value' => !empty($field['prefix']) ? $field['prefix'] : '',
'#description' => t('Define a string that should be prefixed to the value, like $ or €. Leave blank for none. Separate singular and plural values with a pipe (pound|pounds).'),
);
$form['append']['suffix'] = array(
'#type' => 'textfield',
'#title' => t('Suffix'),
'#size' => 40,
'#maxlength' => 60,
'#default_value' => !empty($field['suffix']) ? $field['suffix'] : '',
'#description' => t('Define a string that should suffixed to the value, like m², m/s², kb/s. Leave blank for none. Separate singular and plural values with a pipe (pound|pounds).'),
);
$fields_info = formatted_number_get_fields_info();
$real_field_types = array_keys(formatted_number_get_fields_info('real'));
$minmax_fields = array(
'min' => array(
'#title' => t('Minimum'),
'#description' => t('Use this option to defined the minimum value that can be accepted for this field. Leave blank for no explicit limitation.'),
),
'max' => array(
'#title' => t('Maximum'),
'#description' => t('Use this option to defined the maximum value that can be accepted for this field. Leave blank for no explicit limitation.'),
),
);
foreach ($minmax_fields as $name => $info) {
$default_attributes = array(
'class' => 'formatted-number',
);
$default_value = isset($field[$name]) ? parse_formatted_number($field[$name]) : '';
if (!is_numeric($default_value)) {
$default_value = '';
}
else {
if (in_array($field['type'], $real_field_types)) {
// Initialize default value with as many decimal digits as necessary.
$decimals = strpos($default_value, '.') !== FALSE ? drupal_strlen(preg_replace('#^.*\\.(.*)$#', '\\1', $default_value)) : 0;
$default_value = format_number($default_value, $decimals);
}
else {
$default_attributes['decimals'] = 0;
$default_value = format_number($default_value, 0);
}
}
$field_maxlength = $fields_info[$field['type']]['maxlength'];
$form[$name] = array(
'#type' => 'textfield',
'#title' => $info['#title'],
'#size' => $field_maxlength + 2,
'#maxlength' => $field_maxlength,
'#default_value' => $default_value,
'#attributes' => $default_attributes,
'#description' => $info['#description'],
);
}
if (in_array($field['type'], $real_field_types)) {
if ($field['type'] == 'formatted_decimal') {
$form['precision'] = array(
'#type' => 'select',
'#title' => t('Precision'),
'#options' => drupal_map_assoc(range(1, 20)),
'#default_value' => is_numeric($field['precision']) && (int) $field['precision'] > 0 ? $field['precision'] : 10,
'#description' => t('The total number of digits to store in the database, including digits to the right of the decimal point.'),
);
}
$form['decimals'] = array(
'#type' => 'select',
'#title' => t('Decimals'),
'#options' => drupal_map_assoc(range(0, $field['type'] == 'formatted_float' ? 20 : 4)),
'#default_value' => is_numeric($field['decimals']) && (int) $field['decimals'] >= 0 ? $field['decimals'] : 2,
'#description' => t('The number of digits to the right of the decimal point.'),
);
}
formatted_number_add_js();
return $form;
case 'validate':
if (!empty($field['min'])) {
if (($field['min'] = parse_formatted_number($field['min'])) === FALSE) {
form_set_error('min', t('"Minimum" must be a valid number.'));
}
else {
if ($field['min'] != '') {
$limit = formatted_number_get_number_limit($field['type'], 'min', $field['precision'], $field['decimals']);
if ((double) $field['min'] < $limit) {
form_set_error('min', t('"Minimum" cannot be lower than !num.', array(
'!num' => format_number($limit, isset($field['decimals']) ? $field['decimals'] : 0),
)));
}
}
}
}
if (!empty($field['max'])) {
if (($field['max'] = parse_formatted_number($field['max'])) === FALSE) {
form_set_error('max', t('"Maximum" must be a valid number.'));
}
else {
if ($field['max'] != '') {
$limit = formatted_number_get_number_limit($field['type'], 'max', $field['precision'], $field['decimals']);
if ((double) $field['max'] > $limit) {
form_set_error('max', t('"Maximum" cannot be greater than !num.', array(
'!num' => format_number($limit, isset($field['decimals']) ? $field['decimals'] : 0),
)));
}
}
}
}
if (is_numeric($field['min']) && is_numeric($field['max']) && $field['max'] < $field['min']) {
form_set_error('min', t('"Minimum" may not be a greater than "Maximum".'));
}
return;
case 'save':
$field_option_names = array(
'prefix',
'suffix',
'min',
'max',
);
$real_field_types = array_keys(formatted_number_get_fields_info('real'));
if (in_array($field['type'], $real_field_types)) {
if ($field['type'] == 'formatted_decimal') {
$field_option_names[] = 'precision';
}
$field_option_names[] = 'decimals';
}
return $field_option_names;
case 'database columns':
$fields_info = formatted_number_get_fields_info();
$db_column = $fields_info[$field['type']]['db_column'];
if ($field['type'] == 'formatted_decimal') {
$db_column += array(
'precision' => isset($field['precision']) ? $field['precision'] : 10,
'scale' => isset($field['decimals']) ? $field['decimals'] : 2,
);
}
$db_column += array(
'not null' => FALSE,
'sortable' => TRUE,
);
return array(
'value' => $db_column,
);
}
}