Views filter for boolean field should use field's labels

Created on 31 October 2022, about 2 years ago
Updated 11 January 2024, 11 months ago

Problem/Motivation

When using a boolean field on an entity type the field can have its "on" and "off" values modified. When this field is exposed as a filter in Views the select field hardcodes "True" and "False" as the option's labels rather than using the values defined in the field.

Steps to reproduce

* Create a content type.
* Add a boolean field to the content type.
* Modify the boolean field's "on" and "off" labels.
* Add a view that lists content.
* Add the field above as a filter.
* Expose the field to the visitor.

Proposed resolution

When a boolean field is added as a filter the labels defined on that field will be used as the options in the selector rather than the default "True" / "False".
This fix will be visible when the field filter is exposed.

Remaining tasks

Work out the appropriate solution.

User interface changes

Boolean fields that have their labels changed via the field UI will use those labels in the Views UI, including when the filter is exposed.

API changes

TBD

Data model changes

TBD

Release notes snippet

TBD

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Views 

Last updated about 12 hours ago

Created by

🇺🇸United States DamienMcKenna NH, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom kiwimind

    @quietone having just tested this and also being able to reproduce the issue, I'm interested in the workaround that you mentioned.

    I'm not sure what you mean by "group filters". The only way this term currently makes sense to me is when reordering filters and assigning them to groups there. I hope I'm missing something obvious.

    Currently without leaning towards custom code I'm unable to see how you might output the field labels rather than the True/False.

    Thank you.

  • 🇮🇳India roshni27

    I was able to reproduce the issue, and I attempted to solve it using group filters. However, I noticed that the values for 'enable' and 'disable' were set to 1 and 2, which is incorrect. We actually need the values to be 0 for 'disable' and 1 for 'enable.' I have attached a screenshot for reference. If I have misunderstood, please correct me.

  • 🇮🇳India Akhil Babu Chengannur

    However, I noticed that the values for 'enable' and 'disable' were set to 1 and 2, which is incorrect.

    This is because key-value options for 'True' and 'False' are hardcoded in getValueOptions() method of BooleanOperator class

        if (!isset($this->valueOptions)) {
          $this->valueOptions = [1 => $this->t('True'), 0 => $this->t('False')];
        }
    

    whereas when grouping is enabled, groupForm() method in FilterPluginBase simply adds the options in the same order as they are added in the UI

        if (!empty($this->options['group_info']['optional']) && !$this->multipleExposedInput()) {
          $groups = ['All' => $this->t('- Any -')];
        }
        foreach ($this->options['group_info']['group_items'] as $id => $group) {
          if (!empty($group['title'])) {
            $groups[$id] = $group['title'];
          }
        }
    
    

    If "Grouping 1" and "Grouping 2" are rearraged, then values for 'enable' and 'disable' will also get changed. But filtering will work correctly.

    When a boolean field is added as a filter the labels defined on that field will be used as the options in the selector rather than the default "True" / "False".
    This fix will be visible when the field filter is exposed.

    Is this the correct approach to fix this problem? What if the boolean field is reused in another content type with another set of labels for the "on" and "off" states. How will we decide which option should be displayed in exposed filter.

    Instead, could we provide a text field in the exposed filter configuration form to customize the values??

  • Status changed to Needs review 11 months ago
  • Status changed to Active 11 months ago
  • 🇳🇱Netherlands Lendude Amsterdam

    #3 and #9 outline nicely why we can't just use the field's labels, because the same field can have multiple sets of labels. So the goal set out in the title I don't think we can do.
    The option set out in #9 where we provide fields in the boolean filter where you can configure this could work, but does that really offer much more value over just using a grouped filter for this? I don't think it does, so "Won't fix" for me, unless we can think of way to determine the values we should be using here

Production build 0.71.5 2024