You are here

public function NumericField::render in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/field/NumericField.php \Drupal\views\Plugin\views\field\NumericField::render()
  2. 9 core/modules/views/src/Plugin/views/field/NumericField.php \Drupal\views\Plugin\views\field\NumericField::render()
1 call to NumericField::render()
NodeNewComments::render in core/modules/comment/src/Plugin/views/field/NodeNewComments.php
2 methods override NumericField::render()
NodeNewComments::render in core/modules/comment/src/Plugin/views/field/NodeNewComments.php
Score::render in core/modules/search/src/Plugin/views/field/Score.php

File

core/modules/views/src/Plugin/views/field/NumericField.php, line 151

Class

NumericField
Render a field as a numeric value.

Namespace

Drupal\views\Plugin\views\field

Code

public function render(ResultRow $values) {
  $value = $this
    ->getValue($values);

  // Check to see if hiding should happen before adding prefix and suffix
  // and before rewriting.
  if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
    return '';
  }

  // After the hide_empty check NULL values should be treated as a 0 value.
  $value = $value ?? 0;
  if (!empty($this->options['set_precision'])) {
    $precision = $this->options['precision'];
  }
  elseif ($decimal_position = strpos($value, '.')) {
    $precision = strlen($value) - $decimal_position - 1;
  }
  else {
    $precision = 0;
  }

  // Use round first to avoid negative zeros.
  $value = round($value, $precision);

  // Test against both integer zero and float zero.
  if ($this->options['empty_zero'] && ($value === 0 || $value === 0.0)) {
    return '';
  }
  $value = number_format($value, $precision, $this->options['decimal'], $this->options['separator']);

  // If we should format as plural, take the (possibly) translated plural
  // setting and format with the current language.
  if (!empty($this->options['format_plural'])) {
    $value = PluralTranslatableMarkup::createFromTranslatedString($value, $this->options['format_plural_string']);
  }
  return $this
    ->sanitizeValue($this->options['prefix'], 'xss') . $this
    ->sanitizeValue($value) . $this
    ->sanitizeValue($this->options['suffix'], 'xss');
}