Commit 223d6f86 authored by KarenS's avatar KarenS

#112204 add format_plural option to number prefix/suffix; add unformatted...

#112204 add format_plural option to number prefix/suffix; add unformatted option to formatters; add ability to use both prefix and suffix
parent 9c18cee8
......@@ -38,20 +38,6 @@ function number_field_settings($op, $field) {
$options[$format] = $opt['label'];
}
}
$form['append_string'] = array(
'#type' => 'textfield',
'#title' => t('Append string'),
'#size' => 60,
'#default_value' => isset($field['append_string']) ? $field['append_string'] : '',
'#description' => t('Define a string that should be appended to the value like $, m², €, m/s², kb/s. Leave blank for none.'),
);
$form['append_position'] = array(
'#type' => 'radios',
'#title' => t('Append string position'),
'#options' => array('prefix' => t('Prefix'), 'suffix' => t('Suffix')),
'#default_value' => isset($field['append_position']) ? $field['append_position'] : 'prefix',
'#description' => t('Define the position of the append string relative to its value, as a prefix: $99,99 or as a suffix: 24,03m'),
);
$form['min'] = array(
'#type' => 'textfield',
'#title' => t('Minimum'),
......@@ -62,6 +48,20 @@ function number_field_settings($op, $field) {
'#title' => t('Maximum'),
'#default_value' => isset($field['max']) ? $field['max'] : '',
);
$form['append']['prefix'] = array(
'#type' => 'textfield',
'#title' => t('Prefix'),
'#size' => 60,
'#default_value' => isset($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' => 60,
'#default_value' => isset($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). '),
);
$form['allowed_values'] = array(
'#type' => 'textarea',
'#title' => t('Allowed values list'),
......@@ -95,7 +95,7 @@ function number_field_settings($op, $field) {
break;
case 'save':
return array('append_string', 'append_position', 'min', 'max', 'allowed_values', 'allowed_values_php');
return array('prefix', 'suffix', 'append_position', 'min', 'max', 'allowed_values', 'allowed_values_php');
case 'database columns':
if ($field['type'] == 'number_integer') {
......@@ -176,6 +176,7 @@ function number_field_formatter_info() {
'fr_0' => array('label' => '9 999', 'field types' => array('number_integer', 'number_decimal')),
'fr_1' => array('label' => '9 999, 9', 'field types' => array('number_decimal')),
'fr_2' => array('label' => '9 999, 99', 'field types' => array('number_decimal')),
'unformatted' => array('label' => 'unformatted', 'field types' => array('number_integer', 'number_decimal')),
);
}
......@@ -188,14 +189,19 @@ function number_field_formatter($field, $item, $formatter, $node) {
return '';
}
$item['value'] = check_plain($item['value']);
if ($formatter == 'unformatted') {
return $item['value'];
}
if ($allowed_values = number_allowed_values($field)) {
$item['value'] = $allowed_values[$item['value']];
if ($allowed_values[$item['value']] != $item['value']) {
return $allowed_values[$item['value']];
}
}
switch ($formatter) {
case 'none':
$value = number_format($item['value'], 0, '.', '');
break;
case 'us_0':
$value = number_format($item['value'], 0, '.', ',');
break;
......@@ -227,15 +233,26 @@ function number_field_formatter($field, $item, $formatter, $node) {
$value = number_format($item['value']);
break;
}
if($field['append_string']) {
if($field['append_position'] == 'prefix') {
return check_plain($field['append_string']) . check_plain($value);
} else {
return check_plain($value) . check_plain($field['append_string']);
$prefixes = explode('|', $field['prefix']);
$suffixes = explode('|', $field['suffix']);
if ($prefixes) {
if (sizeof($prefixes) > 1) {
$prefix = format_plural($item['value'], $prefixes[0], $prefixes[1]);
}
else {
$prefix = $field['prefix'];
}
}
if ($suffixes) {
if (sizeof($suffixes) > 1) {
$suffix = format_plural($item['value'], $suffixes[0], $suffixes[1]);
}
else {
$suffix = $field['suffix'];
}
} else {
return check_plain($value);
}
return $prefix . $value . $suffix;
}
/**
......@@ -253,30 +270,38 @@ function number_widget_info() {
/**
* Implementation of hook_widget().
*/
function number_widget($op, &$node, $field, &$node_field) {
function number_widget($op, &$node, $field, &$items) {
switch ($op) {
case 'form':
$form = array();
// Display the append string (if exists) as prefix or suffix next to the input field
$prefix = '';
$suffix = '';
if ($field['append_string']) {
if($field['append_position'] == 'prefix') {
$prefix = check_plain($field['append_string']);
} else {
$suffix = check_plain($field['append_string']);
$prefixes = explode('|', $field['prefix']);
$suffixes = explode('|', $field['suffix']);
if ($prefixes) {
if (sizeof($prefixes) > 1) {
$prefix = format_plural($item['value'], $prefixes[0], $prefixes[1]);
}
else {
$prefix = $field['prefix'];
}
}
if ($suffixes) {
if (sizeof($suffixes) > 1) {
$suffix = format_plural($item['value'], $suffixes[0], $suffixes[1]);
}
else {
$suffix = $field['suffix'];
}
}
$form[$field['field_name']] = array('#tree' => TRUE);
if ($field['multiple']) {
$form[$field['field_name']]['#type'] = 'fieldset';
$form[$field['field_name']]['#description'] = $field['widget']['description'];
$delta = 0;
foreach ($node_field as $data) {
foreach ($items as $data) {
if (isset($data['value'])) {
$form[$field['field_name']][$delta]['value'] = array(
'#type' => 'textfield',
......@@ -286,8 +311,8 @@ function number_widget($op, &$node, $field, &$node_field) {
'#size' => 20,
'#maxlength' => 11,
'#attributes' => array('class' => 'number'),
'#field_prefix' => $prefix,
'#field_suffix' => $suffix,
'#field_prefix' => '<span class="prefix">'. $prefix .'</span>',
'#field_suffix' => '<span class="suffix">'. $suffix .'</span>',
);
$delta++;
}
......@@ -301,8 +326,8 @@ function number_widget($op, &$node, $field, &$node_field) {
'#size' => 20,
'#maxlength' => 11,
'#attributes' => array('class' => 'number'),
'#field_prefix' => $prefix,
'#field_suffix' => $suffix,
'#field_prefix' => '<span class="prefix">'. $prefix .'</span>',
'#field_suffix' => '<span class="suffix">'. $suffix .'</span>',
);
}
}
......@@ -310,27 +335,27 @@ function number_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']][0]['value'] = array(
'#type' => 'textfield',
'#title' => t($field['widget']['label']),
'#default_value' => isset($node_field[0]['value']) ? $node_field[0]['value'] : '',
'#default_value' => isset($items[0]['value']) ? $items[0]['value'] : '',
'#required' => $field['required'],
'#description' => $field['widget']['description'],
'#size' => 20,
'#maxlength' => 11,
'#attributes' => array('class' => 'number'),
'#field_prefix' => $prefix,
'#field_suffix' => $suffix,
'#field_prefix' => '<span class="prefix">'. $prefix .'</span>',
'#field_suffix' => '<span class="suffix">'. $suffix .'</span>',
);
}
return $form;
case 'process form values':
// Don't save empty fields.
foreach ($node_field as $delta => $item) {
$node_field[$delta]['value'] = preg_replace('@[^0-9\.]@','',$node_field[$delta]['value']);
foreach ($items as $delta => $item) {
$items[$delta]['value'] = preg_replace('@[^0-9\.]@','',$items[$delta]['value']);
}
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
foreach ($items as $delta => $item) {
if ($item['value'] == '' && $delta > 0) {
unset($node_field[$delta]);
unset($items[$delta]);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment