Importing "No" values to Boolean field results in empty
Overview
Contact imports to custom "Yes or No" fields work with values 1 or 0. "Yes" also appears to work.
If you import a "No" value, however, the field will be blank for that contact instead of having the "No"/0 label/value.
Technical details
"strtoboolstr" gets run twice on this data. Once on the raw value returning a boolean and once later in the code here
When you run this on a boolean value (you shouldn't, according to the comments, but perhaps we should check for this), "Yes"/true will look like it's working okay because the preg_match will match for (bool)true but it won't match for (bool)false. I'm sure this has something to do with a PHP oddity I know nothing about but related to the fact that strlen((bool)true)
is 1
and strlen((bool)false)
is 0
.
Proposed solution is:
- Don't run strtoboolstr twice. We don't need that second iteration of it because it's already been run on the data. I'm not sure of any other impacts for this so also:
- Write a test for importing to a Yes/No field with the 6 values that should work (1,0,yes,no,true,false). I've started writing this test following examples but i'm having trouble getting my import data actually in properly to run the assertEquals on. I will open a WIP PR with this test.
Any other thoughts welcome!
Reproduction steps
- Create a custom field with data type "Yes or No"
- Create a csv to import first,last,custom_fieldID with data for your custom field "Yes" and "No"
- Run the import normally through the GUI in the Contacts > Import Contacts workflow
- Observe custom field results on the imported contacts
Expected behaviour
According to code comments, this type of field import should accept "1", "0", "Yes", "No", "true", "false" and they should all fill a "Yes or No" field correctly.
Environment information
This was reproduced on the master branch in d9 (5.56.alpha1)