See http://drupal.stackexchange.com/questions/77200/filtering-a-view-by-a-ch...

Create a node type with a checkbox field, but don't define any values for that field (the node type defaults to "1" and "0" as values). Then create a view that filters on that field: rather than allowing you to select "1" or "0", the select box where you usually pick the value the filter should filter by is blank. It should contain "1" and "0".

CommentFileSizeAuthor
#1 764392_0.patch930 bytesdfletcher

Comments

dfletcher’s picture

StatusFileSize
new930 bytes

Ran into this myself, was going to post an issue but found yours and decided since I was not the only one seeing this to dig deeper.

Not quite sure this is the best way to fix this but here's an attempt.

Trouble is that views_handler_filter_field_list handles all fields in a generic uniform way using the core API function list_allowed_values(). In the case of a boolean value, these "allowed values" are boolean TRUE and FALSE. When PHP tries to display TRUE and FALSE as a string, it outputs blank nothing (PHP quirk).

So my patch just says "if there is $this->value_options[0] and $this->value_options[1] but the value is empty, put "True" and "False".

Another approach might be to actually detect if the field data type is boolean and act on that. Other suggestions?

dfletcher’s picture

Okay so I'm really not happy with my solution in the first patch. There could be some other legitimate reason that value is present but empty (actual allowed value of empty string). I'm leaning toward "detect boolean field, do special jiggery for it".

merlinofchaos’s picture

Honestly, I think this is actually a problem with the checkbox field more than Views, which should have sane default. As such, that's how we should consider addressing it: either fixing checkbox field to be sane, or putting a shim in and admitting that checkboxes are broken in this instance.

dfletcher’s picture

Might actually have a reasonable fix for this in core, we'll see how well it goes. Idea is to put defaults in the boolean values fields so that our use case of "making a boolean field as a Views toggle switch" is less clumsy.

Fix here: For boolean with empty values, list_allowed_values() should return array('0', '1')

Edit: oh and I was wrong about the PHP quirk. Turns out it was just that the defaults are currently intentionally blank.

jim0203’s picture

Status: Active » Closed (duplicate)

Sorry guys, I think I shouldn't have created this issue in the first place. Seems like it's already being dealt with as a core issue at https://drupal.org/node/1750950

dfletcher’s picture

That's great thanks Jim, nice find. Looks like it's even scheduled to be backported, perfect.

leoklein’s picture

Still, saved my butt (for the moment). I had a boolean field in D7 for a event calendar/map:

  • Type: boolean
  • Widget: Single on/off checkbox
  • Label: "Public"
  • Help text: "If checked, event will appear on public listings and web pages."
  • Check Use field label instead of the "On value" as label
  • Selected by Default

Views just wouldn't pick up it as a filter (not that it's a Views problem). That was a bummer since I already had about 500 events. The patch helped. Thanks.