I'm creating a website where users can select during registration what positions they play on the field using checkboxes. I have a 'Player Profiles' page to show the team.

I'm using the field rewrite to rewrite what the position is, but when I select "Hide if Empty" and "Count the number 0 as empty" the field is always displayed.

I did some digging and it seems to be trying to evaluate the output of the Boolean field as empty, but since it gets rewritten, it is never 0 or empty.

I added a new type of output 'raw' that will return the raw state of the checkbox and then it can be evaluated and hidden if necessary.

In views_handler_filter_boolean_operator.inc

  function get_value_options() {
    if (isset($this->definition['type'])) {
      if ($this->definition['type'] == 'yes-no') {
        $this->value_options = array(1 => t('Yes'), 0 => t('No'));
      }
      if ($this->definition['type'] == 'on-off') {
        $this->value_options = array(1 => t('On'), 0 => t('Off'));
      }
      if ($this->definition['type'] == 'raw') {
        $this->value_options = array(1 => 1, 0 => 0);
      }
    }

    // Provide a fallback if the above didn't set anything.
    if (!isset($this->value_options)) {
      $this->value_options = array(1 => t('True'), 0 => t('False'));
    }
  }

And in
views_handler_field_boolean.inc

    $form['type'] = array(
      '#type' => 'select',
      '#title' => t('Output format'),
      '#options' => array(
        'yes-no' => t('Yes/No'),
        'true-false' => t('True/False'),
        'on-off' => t('On/Off'),
        'raw' => t('Raw'),
      ),
      '#default_value' => $this->options['type'],
    );

and

    switch ($this->options['type']) {
      case 'yes-no':
      default:
        return $value ? t('Yes') : t('No');
      case 'true-false':
        return $value ? t('True') : t('False');
      case 'on-off':
        return $value ? t('On') : t('Off');
      case 'raw':
        return $value;
    }

Tried to create a patch, but still butchering it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jjarvis98’s picture

Component: Documentation » Miscellaneous
jjarvis98’s picture

Title: "Hide if empty" doesn't work with profile checkboxes. » "Hide if empty" doesn't work with profile checkboxes & Rewrite

Still an issue with the latest version.
The code for views_handler_filter_boolean_operator.inc stays the same.

The code for views_handler_field_boolean.inc just needs to be changed to this:

    $default_formats = array(
      'yes-no' => array(t('Yes'), t('No')),
      'true-false' => array(t('True'), t('False')),
      'on-off' => array(t('On'), t('Off')),
      'raw' => array(t('Raw'))
    );
dawehner’s picture

Please create a patch.

See http://drupal.org/patch/create

Its not that hard :)

webservant316’s picture

This thread was very helpful to me.
Unfortunately I do not have the handy tools to quickly make a patch myself either.
Here is what I did...I slight modification of the above.

In views_handler_filter_boolean_operator.inc insert at line 50

if ($this->definition['type'] == 'yes-0') {
$this->value_options = array(1 => t('Yes'), 0 => t('0'));
}

In views_handler_field_boolean.inc insert at line 34
'yes-0' => array(t('Yes'), t('0')),

This patch enabled me to choose not to display checkbox fields when they were 0 valued.

jjarvis98’s picture

FileSize
1.38 KB

Using Views 6.x-3.0-alpha3.

Extract to your modules directory.
Save the patch file to your modules directory.

patch -p0 < views_hide_if_empty.patch
patching file views/handlers/views_handler_field_boolean.inc
patching file views/handlers/views_handler_filter_boolean_operator.inc
merlinofchaos’s picture

Status: Active » Needs review

Setting proper status for patches.

lhubbert’s picture

This did it for me, spent hours trying to figure out how to work around this very problem. Thanks so much!

dawehner’s picture

FileSize
1.29 KB

This patch showed nothing in the field configuration see

+      'raw' => t('Raw'),

Here is a new patch.

dawehner’s picture

The rest :) looked fine.

bathizte’s picture

You saved me quite some time with this patch .. Thanks !!

merlinofchaos’s picture

Status: Needs review » Needs work

Question: Would it be better to just have a "Custom" value that allows the user to enter the true/false values? That way it could easily be set to an empty string and 'hide if empty' would then work. It shouldn't be that hard to do this.

I think I would much rather see that than 'raw' which isn't all that useful to most people.

dawehner’s picture

Here is a patch for the custom part of the field.

liquidcms’s picture

Version: 6.x-2.8 » 6.x-2.x-dev

title says rewrite of fields is also part of this; but not sure from this if it is or if this patch ever got committed.

i had a site with an older rev of 6.2; but then updated to a Feb 2011 version of 2.x-dev when the site was being updated.. and now i have a view with a pile of fields that are rewritten empty fields.

i just grabbed latest 2.x-dev and no help.. so i'll assume that is this issue and update ver number here.

liquidcms’s picture

maybe title here is just wrong.. not sure rewrite has anything to do with this.. anyway.. my issue was resolved with this patch: http://drupal.org/node/1020540#comment-4417516 which sadly is not committed yet

webservant316’s picture

FYI - I am still depending upon my patch in #4 and so manually rolling it forward as I upgrade my Drupal install to Views 2.16.

tamarackmedia’s picture

I think I was experiencing the same problem and here was my work around without using a patch (forgive me if this isn't actually addressing the issues above, but may be helpful to someone else.

I needed to provide a single checkbox with the active result being a re-write and the inactive result being nothing. Even with Count The Number 0 as Empty enabled, as described above, all nodes were displaying with the re-write.

I was using the simple checkbox widget. I changed to checkbox / radio buttons, added only one value to the available values field, and set options to unlimited, so I still had the checkbox. This allowed me to re-write those that were checked and exclude the rest from display.

Dropper’s picture

Thank you very much! You just saved my day! I didn't enter any values for the single on/off checkbox (it said, it would enter 0 or 1). Now I have entered the 1 and 0 in the configuration fields and now views recognizes the 0 and the 1. Perfect!

Chris Matthews’s picture

Status: Needs work » Closed (outdated)

The Drupal 6 branch is no longer supported, please check with the D6LTS project if you need further support. For more information as to why this issue was closed, please see issue #3030347: Plan to clean process issue queue