You are here

function number_field_formatter in Content Construction Kit (CCK) 5

Implementation of hook_field_formatter().

File

./number.module, line 186
Defines numeric field types.

Code

function number_field_formatter($field, $item, $formatter, $node) {

  // If the value is empty, we have $item['value'] = NULL
  if (is_null($item['value'])) {
    return '';
  }
  $item['value'] = check_plain($item['value']);
  if ($formatter == 'unformatted') {
    return $item['value'];
  }
  if ($allowed_values = number_allowed_values($field)) {
    if ($allowed_values[$item['value']] != $item['value']) {
      return $allowed_values[$item['value']];
    }
  }
  switch ($formatter) {
    case 'us_0':
      $value = number_format($item['value'], 0, '.', ',');
      break;
    case 'us_1':
      $value = number_format($item['value'], 1, '.', ',');
      break;
    case 'us_2':
      $value = number_format($item['value'], 2, '.', ',');
      break;
    case 'be_0':
      $value = number_format($item['value'], 0, ',', '.');
      break;
    case 'be_1':
      $value = number_format($item['value'], 1, ',', '.');
      break;
    case 'be_2':
      $value = number_format($item['value'], 2, ',', '.');
      break;
    case 'fr_0':
      $value = number_format($item['value'], 0, ', ', ' ');
      break;
    case 'fr_1':
      $value = number_format($item['value'], 1, ', ', ' ');
      break;
    case 'fr_2':
      $value = number_format($item['value'], 2, ', ', ' ');
      break;
    default:
      $value = $item['value'];
      break;
  }
  $prefixes = array_map('content_filter_xss', explode('|', check_plain($field['prefix'])));
  $suffixes = array_map('content_filter_xss', explode('|', check_plain($field['suffix'])));
  if ($prefixes) {
    if (sizeof($prefixes) > 1) {
      $prefix = format_plural($item['value'], $prefixes[0], $prefixes[1]);
    }
    else {
      $prefix = $prefixes[0];
    }
  }
  if ($suffixes) {
    if (sizeof($suffixes) > 1) {
      $suffix = format_plural($item['value'], $suffixes[0], $suffixes[1]);
    }
    else {
      $suffix = $suffixes[0];
    }
  }
  return $prefix . $value . $suffix;
}