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

dawehner’s picture

Issue tags: +Needs tests

If 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.

rlmumford’s picture

What do you mean? Like a simpletest? Also, Isn't it obviously broken?

dawehner’s picture

Yes i mean a simpletest. Well obvious for you and now, but writing a test will ensure safety in the future.

rlmumford’s picture

So just checking that I've understood the tests correctly, in views_handler_field.test we have the following lines:

 // Test zero as an integer.
    $view->result[0]->{$column_map_reversed['name']} = 0;
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, 0 should not be treated as empty.'); 

Although this is apparently testing integers, because its testing the 'name' field it isn't testing the views_handler_numeric plugin. Should it be?

rlmumford’s picture

StatusFileSize
new10.45 KB

Here's a patch that adds a test.

rlmumford’s picture

StatusFileSize
new11.21 KB

And here's a patch that fixes the problem.

rlmumford’s picture

StatusFileSize
new11.02 KB

Lets include the test file in .info so it actually runs =/

Status: Needs review » Needs work

The last submitted patch, 178304-numerc_fields_test-7.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new11.72 KB

Here's the patch to fix.

Status: Needs review » Needs work

The last submitted patch, 178304-numerc_fields_empty-8.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new3.26 KB

This 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.

Status: Needs review » Needs work

The last submitted patch, 178304-numerc_fields_test-11.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new3.26 KB

Status: Needs review » Needs work

The last submitted patch, 178304-numerc_fields_test-13.patch, failed testing.

rlmumford’s picture

StatusFileSize
new3.84 KB

Now that I'm happy with the test. Here's a patch to fix it.

rlmumford’s picture

Status: Needs work » Needs review

Sorry for all the faff in this issue.

Status: Needs review » Needs work

The last submitted patch, 178304-numerc_fields_test-15.patch, failed testing.

darren oh’s picture

Status: Needs work » Needs review
StatusFileSize
new12.37 KB

I 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.

Status: Needs review » Needs work

The last submitted patch, views-rewrite-if-empty-numeric-1782304-18.patch, failed testing.

darren oh’s picture

Here’s a patch with updated tests.

darren oh’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, views-rewrite-if-empty-numeric-1782304-20.patch, failed testing.

darren oh’s picture

Status: Needs work » Needs review
StatusFileSize
new4.68 KB

Fixed tests to match expected behavior.

rlmumford’s picture

I 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.

Surely 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.

darren oh’s picture

#15: 178304-numerc_fields_test-15.patch queued for re-testing.

andrewbelcher’s picture

Here's the patch re-rolled for HEAD as the previous one no longer applies.

andrewbelcher’s picture

Sorry, let's try that again...

rob230’s picture

I 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.

kreynen’s picture

Here 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:

  • enable Image, Views and Views UI on a fresh install
  • add an image field to a content type (Field Type: Image/Widget Image)
  • start adding a node and add an image to that node
  • save the node
  • start adding a node and add an image to that node
  • DO NOT save the node

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...

SELECT file_managed.filename AS file_managed_filename, file_managed.fid AS fid, file_managed.uri AS file_managed_uri, file_usage.count AS file_usage_count
FROM 
{file_managed} file_managed
LEFT JOIN {file_usage} file_usage ON file_managed.fid = file_usage.fid
WHERE (( (file_usage.count = '0') ))

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.

jdcreativity’s picture

Issue summary: View changes
Issue tags: +cmd-release-blocker
jdcreativity’s picture

Issue tags: +cmd
kreynen’s picture

Issue tags: -cmd-release-blocker, -cmd

we've found a work around for this in cmdrupal

darren oh’s picture

chris matthews’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

The 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.

Checking patch handlers/views_handler_field_numeric.inc...
error: while searching for:
      }
    }

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

error: patch failed: handlers/views_handler_field_numeric.inc:120
error: handlers/views_handler_field_numeric.inc: patch does not apply
Checking patch views.info...
Hunk #1 succeeded at 277 (offset 12 lines).
andrew answer’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new4.08 KB

Patch rerolled.

damienmckenna’s picture

Issue tags: -Needs tests
Parent issue: » #3054023: Plan for Views 7.x-3.24 release
StatusFileSize
new3.34 KB

Just the tests.

damienmckenna’s picture

damienmckenna’s picture

damienmckenna’s picture

So 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.

damienmckenna’s picture

Status: Needs review » Needs work
damienmckenna’s picture

Removing this from the queue for the next release because the tests don't fail as expected.

FireHawkX’s picture

I 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.