Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Views fields that use the numeric field plugin cannot render float values between zero and negative one.
Values of this range will be rendered as positive instead of negative. E.g. -0.12
will be displayed as 0.12
This is due to the way NumericField::render()
rounds the number first, and then adds back on the remainder:
$remainder = abs($value) - intval(abs($value));
$value = $value > 0 ? floor($value) : ceil($value);
From here our value of -0.12
will be 0
.
$value = number_format($value, 0, '', $this->options['separator']);
if ($remainder) {
// The substr may not be locale safe.
$value .= $this->options['decimal'] . substr($remainder, 2);
}
Proposed resolution
Re-factor NumericField::render()
so the correct precision is passed through to number_format()
Remaining tasks
Re-factorNumericField::render()
Add tests that include negative ranges -0.xx- Review and commit patch
User interface changes
None
API changes
None
Data model changes
None
Beta phase evaluation
Issue category | Bug because rendered output does not match expected output |
---|---|
Issue priority | Normal because only a small number of input values would be affected. |
Comment | File | Size | Author |
---|---|---|---|
#47 | 1952926-47.patch | 7.12 KB | Mirnaxvb |
Comments
Comment #1
portico CreditAttribution: portico commentedHere is the patch.
Comment #3
dawehnerLet's fix this properly in Drupal Core first, with tests etc.
Comment #4
portico CreditAttribution: portico commentedComment #5
portico CreditAttribution: portico commented#1: support_negative-1952926-1.patch queued for re-testing.
Comment #7
shyam kumar kunkala CreditAttribution: shyam kumar kunkala commentedHere is the patch
Comment #8
shyam kumar kunkala CreditAttribution: shyam kumar kunkala commentedHere is the patch
Comment #10
shyam kumar kunkala CreditAttribution: shyam kumar kunkala commentedHere is patch in specified format
Comment #12
shyam kumar kunkala CreditAttribution: shyam kumar kunkala commentedComment #14
fonant CreditAttribution: fonant commentedSee also: #2332743: views_handler_field_numeric error for float values between zero and negative one. in the Views issue queue.
Comment #15
fonant CreditAttribution: fonant commentedComment #16
dcam CreditAttribution: dcam commentedIssues must be fixed in Drupal 8 first, then backported.
Comment #17
gigabates CreditAttribution: gigabates commentedSimpler method using string length after decimal point to determine default precision. This also fixes a floating point precision issue where '1000.1234' would render as '1,000.12339999999995'. Includes tests for both issues.
Comment #18
dawehnerI really like how this is rewritten! This is absolutely cleaner, I wonder whether number_format maybe changed at some point in time?
This should be "Contains \"... just for consistency
Comment #19
jhedstromNeeds work per #18.
Comment #20
tadityar CreditAttribution: tadityar commentedchanged to contains
Comment #21
LendudeAdded some more tests to the ones given in #17, there is more that needs testing with this handler.
Added tests for 'hide_empty' and 'precision'. The precision tests also failed for #17 because of
Needs to be $this->options['precision'];
Also the empty check was done after the rewriting instead of before rewriting so a value of 0 would not be considered empty with a precision set because it would be changed before the !==0 check.
Will add failing tests and patch, interdiff is against #17
Had no internet in the train and missed that some more work had been done since yesterday, but #20 did not address the extra issues I saw, so will post my patch.
Comment #25
finneComment #26
finneRerolled the patch to work with the latest 8.0.x. Especially the renamed file NumericField.php.
Comment #27
Leon Kessler CreditAttribution: Leon Kessler commentedRe-rolling patch with #2546416: Rename ViewUnitTestBase to ViewKernelTestBase
Also updated issue summary and title.
A quick note for anyone who wants to manually test this: since #2342045: Standard views base fields need to use same rendering as Field UI fields, for formatting, access checking, and translation consistency there are no entity fields that use the numeric plugin - so you cannot test with a number entity field, you will need to use a views field that actually uses the numeric plugin. Currently only the file count field and user id field does (and not the node id field).
As these fields will never contain negative values, this bug will only affect fields that are using the numeric field plugin outside of core.
Comment #28
Leon Kessler CreditAttribution: Leon Kessler commentedComment #29
LendudeThanks for the reroll.
Added beta eval.
Expanded the test coverage a bit to also include the decimal and separator options, since these are not covered by tests at the moment and they should be.
Comment #30
Leon Kessler CreditAttribution: Leon Kessler commentedNew patch looks good.
This should be unassigned now, hopefully someone can pick this up and RTBC.
Comment #34
dawehnerNitpick: :)
I assume this needs a reroll in the meantime.
Comment #35
Lendude@dawehner, yup, it did, still applied but needed array conversion and the test needed to be moved to the right spot. Also fixed the nitpick :)
Comment #36
dawehnerDo you mind using
assertEquals
and swapping parameters around?Comment #37
Lendude#36 fixed.
Comment #38
dawehnerThank you @Lendude!
Comment #40
LendudeUnrelated fail
Comment #41
alexpottMoving this to after the hide empty check looks wrong. Because if you set the precision to 0 and the value is 0.1 before it would be hidden if you have the empty zero option.
Comment #42
alexpottHere's a test and fix to show what I'm on about. Also https://twitter.com/alexpott/status/856989395991265284
Comment #43
Lendude#42 seems related to #1232920: Empty rounded numeric fields not hidden properly, that focusses on decimal fields though.
Comment #44
alexpottWe don't need to use preg_match - https://3v4l.org/q4VKG ?!?!?
More tests added.
Comment #45
Lendude@alexpott thanks for all the test coverage!
Had some minor formatting/comment nitpicks, but in order to see if we have all the possible scenario's covered, I decided to rewrite the testing using a dataprovider.
This just addressed some nits and is a 1-1 rewrite of the tests, nothing new added.
Comment #46
jibranPatch looks solid just minor observation.
We should add empty sting here because it is also testing hide empty. I know we added it blow but we can add it here too.
Comment #47
Mirnaxvb CreditAttribution: Mirnaxvb as a volunteer and at Synetic commentedDoing core mentoring with @Lendude.
Fixed feedback from #46.
Comment #48
jibranThanks, @Lendude and @Mirnaxvb
Comment #51
catchCommitted/pushed to 8.4.x and cherry-picked to 8.3.x. Thanks!
Great how this simplifies the code too.