function NumericField::render

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/field/NumericField.php \Drupal\views\Plugin\views\field\NumericField::render()
  2. 8.9.x core/modules/views/src/Plugin/views/field/NumericField.php \Drupal\views\Plugin\views\field\NumericField::render()
  3. 11.x core/modules/views/src/Plugin/views/field/NumericField.php \Drupal\views\Plugin\views\field\NumericField::render()

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.