Problem/Motivation

1. Create a FieldAPI field on an entity, and select the option "Use field label instead of the "On value" as label"
2. Create a view of this entity type
3. Add the boolean field as a filter
4. Problem: in the options popup for the filter handler, the 'Options' select box shows:

- SelectAll
- [BLANK]
- [BLANK]

Short of guessing at of the blank options and then seeing what effect it has on the query, it's impossible to use this.

Proposed resolution

Fix it.

Remaining tasks

User interface changes

The boolean field will work again with views.

API changes

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because the display for boolean fields in views is not working as expected
Prioritized changes The main goal of this issue is usability

Data model changes

Comments

joachim’s picture

The problem is in views_handler_filter_field_list:

  function get_value_options() {
    $field = field_info_field($this->definition['field_name']);
    $this->value_options = list_allowed_values($field);
  }

This returns an array whose keys are 0 and 1 but whose values are empty.

joachim’s picture

In FieldAPI, this is picked up by options_field_widget_form(), where options modules provides the form element for its widgets:

      if ($instance['widget']['settings']['display_label']) {
        $element['#title'] = $instance['label'];
      }

Which is a bit brittle, as it makes this a special case that list_allowed_values() doesn't actually know about.

We probably need a custom handler here for boolean fields to take care of this.

joachim’s picture

The plot thickens somewhat...

The field settings have:

      'settings' => array(
        'allowed_values' => array(
          0 => '',
          1 => '',
        ),

but the 'use field label for on value' is a widget setting, and hence is on the instance:

      'settings' => array(
        'allowed_values' => array(
          0 => '',
          1 => '',
        ),

So it's possible for the field to have no display values for the checkbox, but not have the 'use field label for on value'. However, if you do that then your field widget looks totally broken, so I don't think it's a case we need to cater for.

dave reid’s picture

dublutz’s picture

This problem can be solved also by setting explicit values for On value and Off value in Boolean checkbox field configuration form. Values can be 1 and 0 but they need to be set.

joachim’s picture

Version: 7.x-3.5 » 7.x-3.x-dev
Issue summary: View changes
StatusFileSize
new93.06 KB

Closing #1250794: 1 and 0 not visible when configuring boolean filter criteria as a duplicate. This is newer, but has more information about a potential fix.

joachim’s picture

Status: Active » Needs review
StatusFileSize
new2.85 KB

Here's a patch.

star-szr’s picture

Just ran into this, thanks @joachim. Patch works as expected. Does this need tests?

dawehner’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 7.x-3.x-dev » 8.x-dev
Component: fieldapi data » views.module
Status: Needs review » Patch (to be ported)

The only fun thing is to bring this into Drupal 8, contrib has to be pragmatic!
Committed and pushed to 7.x-3.x

It is great to see a patch from @joachim, and tested by @cottser

mpdonadio’s picture

Assigned: Unassigned » mpdonadio
Issue tags: +VDC

I'll tackle this.

mpdonadio’s picture

Status: Patch (to be ported) » Needs review
Issue tags: +Needs tests, +Needs issue summary update, +Needs beta evaluation
StatusFileSize
new1.45 KB

OK, added field formatter. It works in Views. For some reason, though, the label shows up as 'node.field_the_machine_name' instead of the label in the formatter settings there. I am sure I am missing something silly here.

This will need an IS update and Beta Eval for D8. And tests updates to

  • Drupal\field\Tests\Boolean\BooleanFormatterSettingsTest
  • Drupal\field\Tests\Boolean\BooleanFormatterTest
  • Drupal\views\Tests\Handler\FieldBooleanTest

Yay.

mpdonadio’s picture

Assigned: mpdonadio » Unassigned

Unassigning myself. My availability the next two or three weeks may be spotty.

jhedstrom’s picture

Issue summary: View changes
Issue tags: -Needs tests, -Needs issue summary update, -Needs beta evaluation
StatusFileSize
new5.08 KB
new5.08 KB
new6.53 KB

This adds tests. I don't think the FieldBooleanTest is actually using the Field API, but I added a label test there nonetheless.

jhedstrom’s picture

Issue summary: View changes

The last submitted patch, 13: boolean-fieldapi-1831622-13-TEST-ONLY.patch, failed testing.

jibran’s picture

Title: boolean FieldAPI field with 'Use field label instead of the "On value" as label' option shows no labels Views filter options » Boolean field with 'Use field label instead of the "On value" as label' option shows no label in views filter options
+++ b/core/modules/field/src/Tests/Boolean/BooleanFormatterTest.php
@@ -117,6 +125,10 @@ public function testBooleanFormatter() {
+    // @todo test for empty value for 0 result?

Let's do that as well. Other then that I think this is ready.

mpdonadio’s picture

Addressed #16. Passes locally.

Had to rework the test a smidge. ->assertRaw() can't test for empty strings, and rather than add in a special case, I changed the test to get the rendered content and use ->assertIdentical(). However, because ->render() doesn't return a string, rather a SafeString, I added an explicit cast.

Not sure if it is worth it to file a followup about wrong @return types in KernelTestBase as it is deprecated in favor of the new one.

The last submitted patch, 17: boolean_field_with_use-1831622-17-TEST-ONLY.patch, failed testing.

lendude’s picture

Issue summary: View changes
Status: Needs review » Needs work
StatusFileSize
new12.2 KB

Trying to recreate the original issue in D8:

Boolean field doesn't have "Use field label instead of the "On value" as label" in D8

And the filter options in Views look like this for a boolean field:

So I don't think this issue actually exists in D8 (anymore?). It would be better if the filter value would use a select instead of a textfield, but that seems like a different issue.
Or am I missing some steps to reproduce this? This is with a minimal install with just Views, Views UI and Fields UI added.

mpdonadio’s picture

#19, do you think our test-only patch is an invalid case then? I just retriggered retests of #17 to see what happens.

The last submitted patch, 17: boolean_field_with_use-1831622-17-TEST-ONLY.patch, failed testing.

lendude’s picture

@mpdonadio, I'm just saying that what appears to get fixed here has little or nothing to do with what's in the issue summary. So if this an issue that can be reproduced through the UI some steps to reproduce would be nice.

On to the actual patch.
Like you said in #11, the label still shows up as node.field_the_machine_name in the 'Output format' selector. Would that also need a test?

+++ b/core/modules/system/src/Tests/Common/AttachedAssetsTest.php
@@ -45,7 +45,7 @@ class AttachedAssetsTest extends KernelTestBase {
+  public static $modules = ['language', 'simpletest', 'common_test', 'system'];

@@ -392,6 +392,11 @@ function testRenderDifferentWeight() {
+    // Ensure the locale module's js alter works with external sources.
+    $this->enableModules(['locale']);
+    $this->installSchema('locale', ['locales_source', 'locales_location']);
+    $build['#attached']['library'][] = 'common_test/external';
+

This test seems totally unrelated to this issue.

oenie’s picture

It seems to me that current stable 8.0 version, there is something wrong with the formatter used by Views for the boolean type.
It has already been mentioned in another bug report as well: #2469553

oenie’s picture

After digging a little further, it seems what the old 'properties' of D7, now called base fields, are handled somewhat differently from custom Entity Fields.

The base field 'status' is added the correct filter of boolean in the switch statement in the function mapSingleFieldViewsData in core/modules/views/src/EntityViewsData.php. The code there is using the field type to determine the views configuration.

A newly created field of type boolean however, is processed in the function views_field_default_views_data in core/modules/views/views.views.inc. Unfortunately that code is using the db column type to decide on the views configuration for the field. Boolean fields are created as tiny int in the database, which ends in the fields getting incorrectly labeled as of the filter type 'numeric'.

mpdonadio’s picture

#26, I think that is more part of the related issue you linked. I think this issue is diagnosed, and that we mainly need a Views maintainer to weigh in here (and to remove the bad hunk that creeped in). Triggered a re-test.

xjm’s picture

Priority: Major » Normal
Issue summary: View changes
Issue tags: +Needs manual testing, +Needs steps to reproduce

The patches in #17 do seem to show a bug that still needs fixing. The fail of the test-only patch is:

fail: [Other] Line 152 of core/modules/field/src/Tests/Boolean/BooleanFormatterTest.php:
Value 'l539LvK6' is identical to value ''.

That sounds about right. I agree we should confirm the bug through the UI though before proceeding. There's also a few other things in the patch that would need to be cleaned up, like a checkPlain() in there etc. https://www.drupal.org/node/2549395

The core committers and Views maintainers (@alexpott, @effulgentsia, @xjm, @dawehner, @tim.plunkett) agreed that this does not need to be a major issue.

xjm’s picture

Hmm, actually it's not clear to me that the retests actually ran. In fact I think they did not. I'd suggest testing locally and re-uploading the patches if it turns out they still apply and work.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Version: 8.9.x-dev » 9.3.x-dev
Issue summary: View changes
Status: Needs work » Closed (cannot reproduce)
Issue tags: -Needs manual testing, -Needs steps to reproduce +Bug Smash Initiative
StatusFileSize
new51.18 KB

I tested this on 9.3.x and was not able to reproduce this error. See attached screenshot.

Therefore, closing as cannot reproduce. If you are experiencing this problem reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").

Thanks!