Skip to content

Commit

Permalink
Issue #6412: Fix wrong display of some float values in Views.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHoebart-ICPDR committed Mar 13, 2024
1 parent 5702200 commit 266a501
Show file tree
Hide file tree
Showing 4 changed files with 428 additions and 26 deletions.
31 changes: 19 additions & 12 deletions core/modules/views/handlers/views_handler_field_math.inc
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,31 @@ class views_handler_field_math extends views_handler_field_numeric {

// The rest is directly from views_handler_field_numeric but because it
// does not allow the value to be passed in, it is copied.
if (!empty($this->options['set_precision'])) {
$value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
}
else {
$remainder = abs($value) - intval(abs($value));
$value = $value > 0 ? floor($value) : ceil($value);
$value = number_format($value, 0, '', $this->options['separator']);
if ($remainder) {
// The substr may not be locale safe.
$value .= $this->options['decimal'] . substr($remainder, 2);
}
}

// Check to see if hiding should happen before adding prefix and suffix.
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(rtrim($value, '0')) - $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']);

// Should we format as a plural.
if (!empty($this->options['format_plural']) && ($value != 0 || !$this->options['empty_zero'])) {
$value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
Expand Down
30 changes: 18 additions & 12 deletions core/modules/views/handlers/views_handler_field_numeric.inc
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,29 @@ class views_handler_field_numeric extends views_handler_field {
return '';
}

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

if (!empty($this->options['set_precision'])) {
$value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
$precision = $this->options['precision'];
}
elseif ($decimal_position = strpos($value, '.')) {
$precision = strlen(rtrim($value, '0')) - $decimal_position - 1;
}
else {
$remainder = abs($value) - intval(abs($value));
$value = $value > 0 ? floor($value) : ceil($value);
$value = number_format($value, 0, '', $this->options['separator']);
if ($remainder) {
// The substr may not be locale safe.
$value .= $this->options['decimal'] . substr($remainder, 2);
}
$precision = 0;
}

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

// Should we format as a plural.
if (!empty($this->options['format_plural'])) {
Expand Down
45 changes: 43 additions & 2 deletions core/modules/views/tests/handlers/views_handler_field_math.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ require_once BACKDROP_ROOT . '/core/modules/views/tests/views_query.test';
* Tests the core views_handler_field_math handler.
*/
class ViewsHandlerFieldMath extends ViewsSqlTest {
function viewsData() {

/**
* {@inheritdoc}
*/
protected $profile = 'testing';

/**
* {@inheritdoc}
*/
protected function viewsData() {
$data = parent::viewsData();
return $data;
}

public function testFieldCustom() {
/**
* Test basic field functionality.
*/
protected function testFieldCustom() {
$view = $this->getBasicView();

// Alter the text of the field to a random string.
Expand All @@ -35,4 +47,33 @@ class ViewsHandlerFieldMath extends ViewsSqlTest {

$this->assertEqual($rand1 + $rand2, $view->style_plugin->get_field(0, 'expression'));
}

/**
* Test rendering of float values in "Global: Math expression" fields.
*/
protected function testMathFloatRender() {
// We need one dummy node of any type for our node based views query.
$type = $this->backdropCreateContentType();
$this->backdropCreateNode(array(
'type' => $type->type,
));
$view = views_get_view('floatval_check');
$this->executeView($view);
$result = $view->result[0];

foreach ($view->field as $name => $view_field) {
if ($name == 'nid') {
continue;
}
// In the view we set the label value to the raw input value (floats), to
// compare rendered output here.
$label = $view->field[$name]->label();
$render = $view->field[$name]->advanced_render($result);
$this->assertIdentical($label, $render, format_string('Expected rendered output to be %label, got %render', array(
'%label' => $label,
'%render' => $render,
)));
}
}

}
Loading

0 comments on commit 266a501

Please sign in to comment.