Project

General

Profile

Paste
Download (14.9 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / field / modules / number / number.module @ b0dc3a2e

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines numeric field types.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function number_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/help#number':
14
      $output = '';
15
      $output .= '<h3>' . t('About') . '</h3>';
16
      $output .= '<p>' . t('The Number module defines various numeric field types for the Field module. Numbers can be in integer, decimal, or floating-point form, and they can be formatted when displayed. Number fields can be limited to a specific set of input values or to a range of values. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
17
      return $output;
18
  }
19
}
20

    
21
/**
22
 * Implements hook_field_info().
23
 */
24
function number_field_info() {
25
  return array(
26
    'number_integer' => array(
27
      'label' => t('Integer'),
28
      'description' => t('This field stores a number in the database as an integer.'),
29
      'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
30
      'default_widget' => 'number',
31
      'default_formatter' => 'number_integer',
32
    ),
33
    'number_decimal' => array(
34
      'label' => t('Decimal'),
35
      'description' => t('This field stores a number in the database in a fixed decimal format.'),
36
      'settings' => array('precision' => 10, 'scale' => 2, 'decimal_separator' => '.'),
37
      'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
38
      'default_widget' => 'number',
39
      'default_formatter' => 'number_decimal',
40
    ),
41
    'number_float' => array(
42
      'label' => t('Float'),
43
      'description' => t('This field stores a number in the database in a floating point format.'),
44
      'settings' => array('decimal_separator' => '.'),
45
      'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
46
      'default_widget' => 'number',
47
      'default_formatter' => 'number_decimal',
48
    ),
49
  );
50
}
51

    
52
/**
53
 * Implements hook_field_settings_form().
54
 */
55
function number_field_settings_form($field, $instance, $has_data) {
56
  $settings = $field['settings'];
57
  $form = array();
58

    
59
  if ($field['type'] == 'number_decimal') {
60
    $form['precision'] = array(
61
      '#type' => 'select',
62
      '#title' => t('Precision'),
63
      '#options' => drupal_map_assoc(range(10, 32)),
64
      '#default_value' => $settings['precision'],
65
      '#description' => t('The total number of digits to store in the database, including those to the right of the decimal.'),
66
      '#disabled' => $has_data,
67
    );
68
    $form['scale'] = array(
69
      '#type' => 'select',
70
      '#title' => t('Scale'),
71
      '#options' => drupal_map_assoc(range(0, 10)),
72
      '#default_value' => $settings['scale'],
73
      '#description' => t('The number of digits to the right of the decimal.'),
74
      '#disabled' => $has_data,
75
    );
76
  }
77
  if ($field['type'] == 'number_decimal' || $field['type'] == 'number_float') {
78
    $form['decimal_separator'] = array(
79
      '#type' => 'select',
80
      '#title' => t('Decimal marker'),
81
      '#options' => array('.' => t('Decimal point'), ',' => t('Comma')),
82
      '#default_value' => $settings['decimal_separator'],
83
      '#description' => t('The character users will input to mark the decimal point in forms.'),
84
    );
85
  }
86

    
87
  return $form;
88
}
89

    
90
/**
91
 * Implements hook_field_instance_settings_form().
92
 */
93
function number_field_instance_settings_form($field, $instance) {
94
  $settings = $instance['settings'];
95

    
96
  $form['min'] = array(
97
    '#type' => 'textfield',
98
    '#title' => t('Minimum'),
99
    '#default_value' => $settings['min'],
100
    '#description' => t('The minimum value that should be allowed in this field. Leave blank for no minimum.'),
101
    '#element_validate' => array('element_validate_number'),
102
  );
103
  $form['max'] = array(
104
    '#type' => 'textfield',
105
    '#title' => t('Maximum'),
106
    '#default_value' => $settings['max'],
107
    '#description' => t('The maximum value that should be allowed in this field. Leave blank for no maximum.'),
108
    '#element_validate' => array('element_validate_number'),
109
  );
110
  $form['prefix'] = array(
111
    '#type' => 'textfield',
112
    '#title' => t('Prefix'),
113
    '#default_value' => $settings['prefix'],
114
    '#size' => 60,
115
    '#description' => t("Define a string that should be prefixed to the value, like '$ ' or '&euro; '. Leave blank for none. Separate singular and plural values with a pipe ('pound|pounds')."),
116
  );
117
  $form['suffix'] = array(
118
    '#type' => 'textfield',
119
    '#title' => t('Suffix'),
120
    '#default_value' => $settings['suffix'],
121
    '#size' => 60,
122
    '#description' => t("Define a string that should be suffixed to the value, like ' m', ' kb/s'. Leave blank for none. Separate singular and plural values with a pipe ('pound|pounds')."),
123
  );
124

    
125
  return $form;
126
}
127

    
128
/**
129
 * Implements hook_field_validate().
130
 *
131
 * Possible error codes:
132
 * - 'number_min': The value is less than the allowed minimum value.
133
 * - 'number_max': The value is greater than the allowed maximum value.
134
 */
135
function number_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
136
  foreach ($items as $delta => $item) {
137
    if ($item['value'] != '') {
138
      if (is_numeric($instance['settings']['min']) && $item['value'] < $instance['settings']['min']) {
139
        $errors[$field['field_name']][$langcode][$delta][] = array(
140
          'error' => 'number_min',
141
          'message' => t('%name: the value may be no less than %min.', array('%name' => $instance['label'], '%min' => $instance['settings']['min'])),
142
        );
143
      }
144
      if (is_numeric($instance['settings']['max']) && $item['value'] > $instance['settings']['max']) {
145
        $errors[$field['field_name']][$langcode][$delta][] = array(
146
          'error' => 'number_max',
147
          'message' => t('%name: the value may be no greater than %max.', array('%name' => $instance['label'], '%max' => $instance['settings']['max'])),
148
        );
149
      }
150
    }
151
  }
152
}
153

    
154
/**
155
 * Implements hook_field_presave().
156
 */
157
function number_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
158
  if ($field['type'] == 'number_decimal') {
159
    // Let PHP round the value to ensure consistent behavior across storage
160
    // backends.
161
    foreach ($items as $delta => $item) {
162
      if (isset($item['value'])) {
163
        $items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
164
      }
165
    }
166
  }
167
}
168

    
169
/**
170
 * Implements hook_field_is_empty().
171
 */
172
function number_field_is_empty($item, $field) {
173
  if (empty($item['value']) && (string) $item['value'] !== '0') {
174
    return TRUE;
175
  }
176
  return FALSE;
177
}
178

    
179
/**
180
 * Implements hook_field_formatter_info().
181
 */
182
function number_field_formatter_info() {
183
  return array(
184
    // The 'Default' formatter is different for integer fields on the one hand,
185
    // and for decimal and float fields on the other hand, in order to be able
186
    // to use different default values for the settings.
187
    'number_integer' => array(
188
      'label' => t('Default'),
189
      'field types' => array('number_integer'),
190
      'settings' =>  array(
191
        'thousand_separator' => '',
192
        // The 'decimal_separator' and 'scale' settings are not configurable
193
        // through the UI, and will therefore keep their default values. They
194
        // are only present so that the 'number_integer' and 'number_decimal'
195
        // formatters can use the same code.
196
        'decimal_separator' => '.',
197
        'scale' => 0,
198
        'prefix_suffix' => TRUE,
199
      ),
200
    ),
201
    'number_decimal' => array(
202
      'label' => t('Default'),
203
      'field types' => array('number_decimal', 'number_float'),
204
      'settings' =>  array(
205
        'thousand_separator' => '',
206
        'decimal_separator' => '.',
207
        'scale' => 2,
208
        'prefix_suffix' => TRUE,
209
      ),
210
    ),
211
    'number_unformatted' => array(
212
      'label' => t('Unformatted'),
213
      'field types' => array('number_integer', 'number_decimal', 'number_float'),
214
    ),
215
  );
216
}
217

    
218
/**
219
 * Implements hook_field_formatter_settings_form().
220
 */
221
function number_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
222
  $display = $instance['display'][$view_mode];
223
  $settings = $display['settings'];
224

    
225
  $element = array();
226

    
227
  if ($display['type'] == 'number_decimal' || $display['type'] == 'number_integer') {
228
    $options = array(
229
      ''  => t('<none>'),
230
      '.' => t('Decimal point'),
231
      ',' => t('Comma'),
232
      ' ' => t('Space'),
233
    );
234
    $element['thousand_separator'] = array(
235
      '#type' => 'select',
236
      '#title' => t('Thousand marker'),
237
      '#options' => $options,
238
      '#default_value' => $settings['thousand_separator'],
239
    );
240

    
241
    if ($display['type'] == 'number_decimal') {
242
      $element['decimal_separator'] = array(
243
        '#type' => 'select',
244
        '#title' => t('Decimal marker'),
245
        '#options' => array('.' => t('Decimal point'), ',' => t('Comma')),
246
        '#default_value' => $settings['decimal_separator'],
247
      );
248
      $element['scale'] = array(
249
        '#type' => 'select',
250
        '#title' => t('Scale'),
251
        '#options' => drupal_map_assoc(range(0, 10)),
252
        '#default_value' => $settings['scale'],
253
        '#description' => t('The number of digits to the right of the decimal.'),
254
      );
255
    }
256

    
257
    $element['prefix_suffix'] = array(
258
      '#type' => 'checkbox',
259
      '#title' => t('Display prefix and suffix.'),
260
      '#default_value' => $settings['prefix_suffix'],
261
    );
262
  }
263

    
264
  return $element;
265
}
266

    
267
/**
268
 * Implements hook_field_formatter_settings_summary().
269
 */
270
function number_field_formatter_settings_summary($field, $instance, $view_mode) {
271
  $display = $instance['display'][$view_mode];
272
  $settings = $display['settings'];
273

    
274
  $summary = array();
275
  if ($display['type'] == 'number_decimal' || $display['type'] == 'number_integer') {
276
    $summary[] = number_format(1234.1234567890, $settings['scale'], $settings['decimal_separator'], $settings['thousand_separator']);
277
    if ($settings['prefix_suffix']) {
278
      $summary[] = t('Display with prefix and suffix.');
279
    }
280
  }
281

    
282
  return implode('<br />', $summary);
283
}
284

    
285
/**
286
 * Implements hook_field_formatter_view().
287
 */
288
function number_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
289
  $element = array();
290
  $settings = $display['settings'];
291

    
292
  switch ($display['type']) {
293
    case 'number_integer':
294
    case 'number_decimal':
295
      foreach ($items as $delta => $item) {
296
        $output = number_format($item['value'], $settings['scale'], $settings['decimal_separator'], $settings['thousand_separator']);
297
        if ($settings['prefix_suffix']) {
298
          $prefixes = isset($instance['settings']['prefix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['prefix'])) : array('');
299
          $suffixes = isset($instance['settings']['suffix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['suffix'])) : array('');
300
          $prefix = (count($prefixes) > 1) ? format_plural($item['value'], $prefixes[0], $prefixes[1]) : $prefixes[0];
301
          $suffix = (count($suffixes) > 1) ? format_plural($item['value'], $suffixes[0], $suffixes[1]) : $suffixes[0];
302
          $output = $prefix . $output . $suffix;
303
        }
304
        $element[$delta] = array('#markup' => $output);
305
      }
306
      break;
307

    
308
    case 'number_unformatted':
309
      foreach ($items as $delta => $item) {
310
        $element[$delta] = array('#markup' => $item['value']);
311
      }
312
      break;
313
  }
314

    
315
  return $element;
316
}
317

    
318
/**
319
 * Implements hook_field_widget_info().
320
 */
321
function number_field_widget_info() {
322
  return array(
323
    'number' => array(
324
      'label' => t('Text field'),
325
      'field types' => array('number_integer', 'number_decimal', 'number_float'),
326
    ),
327
  );
328
}
329

    
330
/**
331
 * Implements hook_field_widget_form().
332
 */
333
function number_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
334
  $value = isset($items[$delta]['value']) ? $items[$delta]['value'] : '';
335
  // Substitute the decimal separator.
336
  if ($field['type'] == 'number_decimal' || $field['type'] == 'number_float') {
337
    $value = strtr($value, '.', $field['settings']['decimal_separator']);
338
  }
339

    
340
  $element += array(
341
    '#type' => 'textfield',
342
    '#default_value' => $value,
343
    // Allow a slightly larger size that the field length to allow for some
344
    // configurations where all characters won't fit in input field.
345
    '#size' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 4 : 12,
346
    // Allow two extra characters for signed values and decimal separator.
347
    '#maxlength' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 2 : 10,
348
    // Extract the number type from the field type name for easier validation.
349
    '#number_type' => str_replace('number_', '', $field['type']),
350
  );
351

    
352
  // Add prefix and suffix.
353
  if (!empty($instance['settings']['prefix'])) {
354
    $prefixes = explode('|', $instance['settings']['prefix']);
355
    $element['#field_prefix'] = field_filter_xss(array_pop($prefixes));
356
  }
357
  if (!empty($instance['settings']['suffix'])) {
358
    $suffixes = explode('|', $instance['settings']['suffix']);
359
    $element['#field_suffix'] = field_filter_xss(array_pop($suffixes));
360
  }
361

    
362
  $element['#element_validate'][] = 'number_field_widget_validate';
363

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

    
367
/**
368
 * FAPI validation of an individual number element.
369
 */
370
function number_field_widget_validate($element, &$form_state) {
371
  $field = field_widget_field($element, $form_state);
372
  $instance = field_widget_instance($element, $form_state);
373

    
374
  $type = $element['#number_type'];
375
  $value = $element['#value'];
376

    
377
  // Reject invalid characters.
378
  if (!empty($value)) {
379
    switch ($type) {
380
      case 'float':
381
      case 'decimal':
382
        $regexp = '@([^-0-9\\' . $field['settings']['decimal_separator'] . '])|(.-)@';
383
        $message = t('Only numbers and the decimal separator (@separator) allowed in %field.', array('%field' => $instance['label'], '@separator' => $field['settings']['decimal_separator']));
384
        break;
385

    
386
      case 'integer':
387
        $regexp = '@([^-0-9])|(.-)@';
388
        $message = t('Only numbers are allowed in %field.', array('%field' => $instance['label']));
389
        break;
390
    }
391
    if ($value != preg_replace($regexp, '', $value)) {
392
      form_error($element, $message);
393
    }
394
    else {
395
      if ($type == 'decimal' || $type == 'float') {
396
        // Verify that only one decimal separator exists in the field.
397
        if (substr_count($value, $field['settings']['decimal_separator']) > 1) {
398
          $message = t('%field: There should only be one decimal separator (@separator).',
399
            array(
400
              '%field' => t($instance['label']),
401
              '@separator' => $field['settings']['decimal_separator'],
402
            )
403
          );
404
          form_error($element, $message);
405
        }
406
        else {
407
          // Substitute the decimal separator; things should be fine.
408
          $value = strtr($value, $field['settings']['decimal_separator'], '.');
409
        }
410
      }
411
      form_set_value($element, $value, $form_state);
412
    }
413
  }
414
}
415

    
416
/**
417
 * Implements hook_field_widget_error().
418
 */
419
function number_field_widget_error($element, $error, $form, &$form_state) {
420
  form_error($element['value'], $error['message']);
421
}