Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
It looks like you cannot use Views filters on boolean type of fields properly because it doesn't use right formatter.
Remaining tasks
- Write a patch to fix ti
- Write tests to not make it break again
User interface changes
-
API changes
-
Comment | File | Size | Author |
---|---|---|---|
#16 | AfterPatch-2469553-15.png | 47.24 KB | greggmarshall |
#16 | BeforePatch-2469553-15.png | 47.95 KB | greggmarshall |
#15 | 2469553-15.patch | 2.27 KB | Lendude |
#15 | interdiff-2469553-11-15.txt | 1.41 KB | Lendude |
#12 | 2469553-views-filter-criteria-form.png | 118.71 KB | mohit_aghera |
Comments
Comment #1
oenie CreditAttribution: oenie as a volunteer commentedAfter 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 correctly labeled as a filter of type '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'.
Why base fields are treated differently from custom fields is a mystery to me :)
To me this is a big issue, since it effectively blocks you from using a boolean field as a filter.
Comment #2
dawehnerThe answer is simply time.
Comment #3
oenie CreditAttribution: oenie as a volunteer commentedOK, i could have guessed that ;)
It works by mimicking the switch-case code from the mapSingleFieldViewsData
Is it an option that i provide something like this as a patch ?
There's plenty of discussion on related subjects that might make this code temporary, but i assume it's better to have something that works now than code that is broken ?
The discussion involve topics and solution that don't seem to be on the horizon right now ...
(f.i. https://www.drupal.org/node/2337515)
Comment #4
dawehnerYeah that issue is indeed dealing with that kind of problem.
Making those mappings more parallel is certainly the right first step to get some small level of sanity there.
Comment #5
oenie CreditAttribution: oenie as a volunteer commentedOk, attached is a first try at integrating the code from the base fields into the hook for the custom fields.
Comment #6
oenie CreditAttribution: oenie as a volunteer commentedComment #7
greggmarshallI can confirm the patch works for Boolean fields, didn't check other field types.
Before:
After:
Comment #8
claudiu.cristeaThe wrapping is 80 characters. You should continue the first line till reach 80 char.
According to our coding standards @see must be before @todo. See https://www.drupal.org/coding-standards/docs#order.
If we are introducing this switch then I guess we'll need to provide tests for each $field_type, right?
What's $field? Seems that is never used.
'the' goes one line up.
No need for the empty line.
Comment #9
geertvd CreditAttribution: geertvd at XIO commentedFixed the issues mentioned in #8 and added some testing (we should probably add more though)
I think that todo is only applicable for schema fields.
These cases don't really make sense for fields, this, I think it only applies to schema fields.
Comment #11
LendudeI'm all for restoring sanity, but hanging this bug up on this seems strange to me.
Can't we just fix this issue and open another issue to get custom fields to be treated the same as base fields?
The patch in #9 for example breaks the 'numeric' field (manual testing revealed this), so like @claudiu.cristea pointed out we would need coverage for all the field types for that to move forward.
Adding a patch that just makes an exception for the boolean field, so the test coverage provided by @geertvd would be sufficient for that change I feel.
Comment #12
mohit_aghera CreditAttribution: mohit_aghera as a volunteer and at Axelerant commentedI tested #11 patch on simplytest.me
Result looks bit weird to me.
As you can see in 2469553-Node-creation-form.png there are radio buttons for the field.
However, for views' filter criteria field setting i can see select list instead of radio buttons 2469553-views-filter-criteria-form.png
May be others should also review it.
Update:
While testing this field, I kept field type as "List (text)" and from "Manage form display" I changed widget to "Checkboxes/Radio buttons"
Comment #13
LendudeYeah this only applies to field type 'Boolean'. Retested this, and looks fine to me.
Edit: 'Fine' meaning, it looks exactly like it does in the screenshot in the Issue summary.
Comment #14
dawehnerIs there a reason this test function doesn't assert anything?
Comment #15
LendudeI think we can just remove that bit of the tests. That was probably intended as a start to test all filter types, but if we want to take this the route of only fixing it for the boolean field, then that is no longer needed.
Comment #16
greggmarshallConfirming patch solves boolean field issue.
Before
After
Comment #17
dawehner#15 looks reasonable for me.
Comment #18
catchDo we want a follow-up for the refactor in #9?
Comment #19
dawehner@catch
Well, ideally this entire bit would be merged with EntityViewsData itself.
Comment #20
catchCommitted/pushed to 8.1.x and cherry-picked to 8.0.x. Thanks!