For fields using views_handler_field_numeric, if the value is empty and there is text in the "rewrite if empty" the value will not be rewritten.
This is largely due to this line:
// 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 '';
}
For some reason the field is only returned empty if hide_empty is set rut not if a rewrite is to be applied. The patch below will allow rewrites to be applied however I wonder if we just want to remove the check against $this->options['hide_empty'].
Comments
Comment #1
dawehnerIf we want to fix something like that we need a test which prooves that there is something broken, sorry
but this will provide much better quality in the future.
Comment #2
rlmumfordWhat do you mean? Like a simpletest? Also, Isn't it obviously broken?
Comment #3
dawehnerYes i mean a simpletest. Well obvious for you and now, but writing a test will ensure safety in the future.
Comment #4
rlmumfordSo just checking that I've understood the tests correctly, in views_handler_field.test we have the following lines:
Although this is apparently testing integers, because its testing the 'name' field it isn't testing the views_handler_numeric plugin. Should it be?
Comment #5
rlmumfordHere's a patch that adds a test.
Comment #6
rlmumfordAnd here's a patch that fixes the problem.
Comment #7
rlmumfordLets include the test file in .info so it actually runs =/
Comment #9
rlmumfordHere's the patch to fix.
Comment #11
rlmumfordThis is probably a better test. It leaves the rewriting stuff up to views_handler_field but does check that views_handler_field_numeric::render returns empty if the value is empty.
Comment #13
rlmumfordComment #15
rlmumfordNow that I'm happy with the test. Here's a patch to fix it.
Comment #16
rlmumfordSorry for all the faff in this issue.
Comment #18
darren ohI think the last patch failed because the hide_empty check was removed. If there is no empty text and hide_empty is not set, the field should be processed normally.
Comment #20
darren ohHere’s a patch with updated tests.
Comment #21
darren ohComment #23
darren ohFixed tests to match expected behavior.
Comment #24
rlmumfordSurely this isn't the case. If the field is empty it should be returned as empty, regardless of whether hide_empty or empty text is checked. There are also options such as hide_alter_empty to think about.
Really this is built the wrong way round, if the value is empty the render() method should return empty regardless of what other options have been set.
Comment #25
darren oh#15: 178304-numerc_fields_test-15.patch queued for re-testing.
Comment #26
andrewbelcher commentedHere's the patch re-rolled for HEAD as the previous one no longer applies.
Comment #27
andrewbelcher commentedSorry, let's try that again...
Comment #28
rob230 commentedI think the current behaviour is definitely not right. There is no way to rewrite the results of an integer field if it's empty. It means the entire "No results behaviour" section is worthless. It doesn't matter what you specify there, it'll always be empty. I haven't tried any of the patches yet.
Comment #29
kreynen commentedHere is a real world bug that the current views_handler_field_numeric causes w/ just Views and core.
Originally I thought the issue I was having filtering by File Usage had to do with the way Media was reporting usage, but @Dave Reid pointed out this happens with any files. The only relation to Media was that the Media File Selector Widget gives you the UI to find and use files from the site that aren't being used up to that point. If you only use the Image widget, all uploaded images are used by the node... unless the node isn't saved. Then you can get images into your file library with 0 usage which can't be found using Views to filter.
The bug is easy to reproduce:
If you look at admin/content/file, there should now be 1 used and 1 un-used file. Now create a File view and filter by usage = 1. That works, but changing usage to = 0 or < 1 does not. That is because the query will look something like...
Looking at the file_usage table, there are only records for used files. Without a row in file_usage for the unused files, file_usage.count needs to be IS NULL for the query to return the unused files.
I'm not sure if there is any easy way to deal with null values in a numeric handler, but I found this attempt to address the issue at the file_entity level.
#2052461: Provide "file in use" views filter
I tried to apply the patch from 8/3/2013 to the current 7.x-3.x branch, but it failed. Looking at why now.
Comment #30
jdcreativity commentedComment #31
jdcreativity commentedComment #32
kreynen commentedwe've found a work around for this in cmdrupal
Comment #33
darren ohComment #34
chris matthews commentedThe latest 5 year old patch in #27 to views_handler_field_numeric.inc and views.info does not apply to the latest views 7.x-3.x-dev.
Comment #35
andrew answer commentedPatch rerolled.
Comment #36
damienmckennaJust the tests.
Comment #39
damienmckennaAdding folks who worked on an identical issue: #1789294: Zero values with decimals not being treated as zero in views_handler_field_numeric
Comment #40
damienmckennaComment #41
damienmckennaSo the tests-only patch should fail, but it passes. Could someone please extend the test to make it fail, so we can confirm it's fixing this bug? Thanks.
Comment #42
damienmckennaComment #43
damienmckennaRemoving this from the queue for the next release because the tests don't fail as expected.
Comment #44
FireHawkX commentedI tried this patch#35, it applied cleanly, but did not solve my problem... the text entered in the no result behavior still does not display.
My number is a decimal field, formatted with 2 precision, using comma and a "space $" suffix...
so the "zero" value is displayed like this: 0,00 $
I have also added this patch to try : https://www.drupal.org/project/views/issues/1239522
But it also did not solve the issue...
I have managed a workaround to get it working by using views conditional, and setting the value less than 1 since in my use case the value will never be 0.99 or below... trying equals or not equals (0) (0,00) (0,00 $), or even greater than 0 did not work with conditional views either... only the less than 1 actually worked.