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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mooffie’s picture

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?

mitchell’s picture

Component: Code » Views integration
Status: Active » Postponed (maintainer needs more info)
quicksketch’s picture

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 (for Drupal 7)
Version: 6.x-1.1 » 6.x-2.x-dev
Component: Views integration » Views Data
Category: bug » feature
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.

cerup’s picture

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

larowlan’s picture

Status: Active » Needs review
FileSize
2.28 KB

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

quicksketch’s picture

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.

larowlan’s picture

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.

ari-meetai’s picture

+1

scarvajal’s picture

Subscribing

bojanz’s picture

Status: Needs review » Needs work

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

mmilo’s picture

FileSize
20.01 KB

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)

mmilo’s picture

Status: Needs work » Needs review
FileSize
19.99 KB

Ugh, left a dpm() in there.

mooffie’s picture

FileSize
2.45 KB

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

bojanz’s picture

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

mmilo’s picture

FileSize
2.42 KB

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

bojanz’s picture

+        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!

mmilo’s picture

Status: Needs work » Needs review
FileSize
2.43 KB

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

kenianbei’s picture

subscribe

merlinofchaos’s picture

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

drupalexio’s picture

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.

dawehner’s picture

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

divined’s picture

and exposed filter support?

divined’s picture

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

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:

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;
  }
David_Rothstein’s picture

Issue summary: View changes

Related issue, but for contextual filters: #2411811: Add an "OR IS NULL" option to contextual filters