Download & Extend

Add "OR IS NULL" as an option to views_handler_filter_numeric

Project:Views
Version:7.x-3.x-dev
Component:Views Data
Category:feature request
Priority:normal
Assigned:Unassigned
Status:patch (to be ported)

Issue Summary

I'm not sure if this is a bug or intentional, but when setting a filter for a flag counter less than 5 (for example), items with 1-4 flags will appear but items without any flags will be left out.

I wanted to use this for an "inappropriate" flag that would automatically remove content after a certain number of users flagged it as inappropriate, but right now the filter only allows "mildly" inappropriate content through...

I'm guessing the result of whatever function is called to retrieve the number of flag counts defaults to NULL when there are no flags associated with a node, but maybe it would be more helpful if the returned value was 0? I'm not a PHP expert, so I'm not sure how difficult this would be to change/if there's a good reason not to...

Comments

#1

That's an SQL issue. Since the "count" column is NULL (for items never flagged), the expression count < 5 evaluates to NULL, which is false. So the record isn't shown.

Things to investigate:
1. How are the CCK people handle this same issue? (I'm refering to CCK's Numeric field)
2. Perhaps a newer views_handler_filter_numeric is smart enough to deal with a NULL?

#2

Component:Code» Views integration
Status:active» postponed (maintainer needs more info)

#3

Title:Flag counter filter in views filters out unflagged content automatically» Add "OR IS NULL" as an option to views_handler_filter_numeric
Project:Flag» Views
Version:6.x-1.1» 6.x-2.x-dev
Component:Views integration» Views Data
Category:bug report» feature request
Status:postponed (maintainer needs more info)» active

Like Mooffie said a while back, this is because of the way SQL works. NULL is not less than 1, it just doesn't exist. To fix this, the Views views_handler_filter_numeric class has to support creating a query that does a "WHERE field < 5 OR field IS NULL". Flag (and many other modules) will then gain the ability to list content that is either less than a certain value or 0.

#4

Is there a way to override this then? I'd also like the ability to filter by 'is below value or null'.

#5

Status:active» needs review

Hi
Patch is attached that adds the following additional operators to the numeric filter:

  • Equal to OR empty
  • Less than OR empty
  • Less than or equal to OR empty
  • Greater than OR empty
  • Greater than or equal to OR empty

Lee

AttachmentSizeStatusTest resultOperations
views-411376.patch2.28 KBIgnored: Check issue status.NoneNone

#6

Perhaps a checkbox to "Include NULL rows" would be preferable to adding 5 additional options in the select list? Thanks for the patch either way though.

#7

yeah good point, I needed this in a hurry and only needed one of the five but after writing it thought I should help others in the same bind out so added the other four, it would have been better your way, if I get time I'll redo it but that's not likely to be till next month at least.

#8

+1

#9

Subscribing

#10

Status:needs review» needs work

quicksketch's #6 is the way to go here, I think.

#11

Here's a patch as per #6, applied to views-6.x-2.12 (6.x-2.x-dev yields an error with relationships and I can't use it, as some refactoring seems to be in progress).

Includes a checkbox that checks for NULL values.

(My knowledge of Views coding is pretty limited, so hopefully this is correct)

AttachmentSizeStatusTest resultOperations
views_or_null.patch20.01 KBIgnored: Check issue status.NoneNone

#12

Status:needs work» needs review

Ugh, left a dpm() in there.

AttachmentSizeStatusTest resultOperations
views_or_null.patch19.99 KBIgnored: Check issue status.NoneNone

#13

@mmilo: Here's a re-roll of your patch. View it to understand that yours wasn't generated correctly.

AttachmentSizeStatusTest resultOperations
views_or_null.patch2.45 KBIgnored: Check issue status.NoneNone

#14

Status:needs review» needs work

allownull needs to be allow_null.
There are coding standard violations (IF without braces for example)

+      '#description' => t('Equates the value of NULL as zero (0).'),

Not valid english. Try "Equates the value of NULL rows to zero." or "Treats NULL as zero."

#15

@bojanz Can't say I agree it's invalid english - it would treat a NULL value the same as if it was a ZERO value. Changed it anyways.
Don't know what you're referring to when you say 'IF without braces' either? Could you point this out in the patch?

@mooffie Odd, I didn't look over the patch, weird it did that.

AttachmentSizeStatusTest resultOperations
views_or_null.patch2.42 KBIgnored: Check issue status.NoneNone

#16

+        if ($this->value['value'] > 0)
+          $is_null = FALSE;

Also:

+  function check_for_null($field)
+  {
+    $is_null = $this->options['allow_null'] ? "OR $field IS NULL" : FALSE;
+    switch ($this->operator)
+    {

Opening brace comes in the same line as the function / switch.

Thanks, we are almost there!

#17

Status:needs work» needs review

Doh, I read "braces" as "parentheses".

AttachmentSizeStatusTest resultOperations
views_or_null_311210.patch2.43 KBIgnored: Check issue status.NoneNone

#18

subscribe

#19

Version:6.x-2.x-dev» 7.x-3.x-dev
Status:needs review» patch (to be ported)

Committed to 6.x-3.x -- doesn't apply to 7.x, for hopefully obvious reasons.

This hilites that the add_where on this filter needs to change. In the New World ORder we aren't allowed to assume an AND when adding multiple where clauses. We need to fix that. Created an issue for that here: http://drupal.org/node/1101876

#20

Version:7.x-3.x-dev» 6.x-3.x-dev

This issue is about 6.x-3.x, not about 7.x-3.x.

#21

Version:6.x-3.x-dev» 7.x-3.x-dev

Well the patch got already committed to 6.x-3.x but wasn't ported to 7.x-3.x yet, so the issue status was correct

#22

and exposed filter support?

#23

For 7.x last patch + change op_simple function, and don't change op_between..

<?php

function op_simple($field) {
   
$is_null = $this->check_for_null($field);
    if (!
$is_null) {
                
$this->query->add_where($this->options['group'], $field, $this->value['value'], $this->operator);
    } else {
       
$this->query->add_where($this->options['group'], db_or()->condition($field, $this->value, $this->operator)->condition($field, NULL, 'IS NULL'));
    }
}   
?>

and change check_for_null function:

<?php
function check_for_null($field) {
   
$is_null = $this->options['allow_null'] ? TRUE : FALSE;
    switch (
$this->operator) {
      case
'>':
       
$is_null = FALSE;
      case
'>=':
      case
'=':
        if (
$this->value['value'] > 0) {
         
$is_null = FALSE;
        }
    }
   
    return
$is_null;
  }
?>
nobody click here