You are here

public function NumericField::render in Drupal 9

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. 10 core/modules/views/src/Plugin/views/field/NumericField.php \Drupal\views\Plugin\views\field\NumericField::render()

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

2 calls to NumericField::render()
NodeNewComments::render in core/modules/comment/src/Plugin/views/field/NodeNewComments.php
Renders the field.
Score::render in core/modules/search/src/Plugin/views/field/Score.php
Renders the field.
2 methods override NumericField::render()
NodeNewComments::render in core/modules/comment/src/Plugin/views/field/NodeNewComments.php
Renders the field.
Score::render in core/modules/search/src/Plugin/views/field/Score.php
Renders the field.

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 '';
  }
  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');
}