-
-
Notifications
You must be signed in to change notification settings - Fork 817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
correct logic for handling empty-array values for checkboxes; #23305
Conversation
(Standard links)
|
... oh, in case it is needed to explain why Not-empty resolves to FALSE when the array member is an empty array, but the test of the same with <?php
$theArray = ['field' => []];
if (isset($theArray['field'])) {
echo 'ISSET';
}
if (empty($theArray['field'])) {
echo 'EMPTY';
} |
@ginkgomzd can you explain to non technical users on how to reproduce this? So we can review this PR next time we are doing our round of PR reviews. |
@jaapjansma okay, I updated the "To Reproduce" section. |
Can this get reviewed again please? |
I'm going to take a look at this today - I think it will resolve some unit test problems I'm having with drupal 10. I also have a partial unit test that could go along with this. I'm not sure the first part of the change here is necessary - $field is a field definition not the value being passed in. |
Noting the patch has conflicts even though github doesn't seem to think so. Let's see what jenkins thinks - jenkins test this please. But yes I agree with the change in formatCheckBoxField() but not the first one about $field, although in practice probably doesn't make any difference - the field will either have a definition or not. |
Jenkins agrees the patch doesn't apply anymore. @ginkgomzd can you update and then I think it's mergeable. |
Something was bugging me so I took another look. I think it needs to have special handling for empty arrays, because otherwise what it stores in the db is index b4b5611269..57c1400afb 100644
--- a/api/v3/utils.php
+++ b/api/v3/utils.php
@@ -1173,15 +1173,21 @@ function formatCheckBoxField(&$checkboxFieldValue, $customFieldLabel, $entity) {
$options = $options['values'];
$validValue = TRUE;
if (is_array($checkboxFieldValue)) {
- foreach ($checkboxFieldValue as $key => $value) {
- if (!array_key_exists($key, $options)) {
- $validValue = FALSE;
- }
- }
- if ($validValue) {
- // we have been passed an array that is already in the 'odd' custom field format
+ if (empty($checkboxFieldValue)) {
+ $checkboxFieldValue = '';
return;
}
+ else {
+ foreach ($checkboxFieldValue as $key => $value) {
+ if (!array_key_exists($key, $options)) {
+ $validValue = FALSE;
+ }
+ }
+ if ($validValue) {
+ // we have been passed an array that is already in the 'odd' custom field format
+ return;
+ }
+ }
}
// so we either have an array that is not keyed by the value or we have a string that doesn't hold separators |
0f6e4e4
to
870e748
Compare
Thanks @demeritcowboy ! I don't think you need to worry about the delimiter handling for empty values, if I'm reading the code correctly, here: Line 1216 in 870e748
Basically, I think it should hit, |
But if you run it and look in the db and compare to what gets stored when using the UI you'll see this doesn't give the right result. |
143e01b
to
0433374
Compare
ah, I see it now. |
Yes that looks good thanks. |
[NFC] php8 - Unit test for empty checkbox array #23305
Seems like passing |
On second thought, |
Included in CiviCRM 5.58.0 PR: civicrm#23305
Included in CiviCRM 5.58.0 PR: civicrm#23305
Overview
API Contact.create has an edge-case for Checkbox custom-fields that does not handle empty arrays properly. There is a comment to this effect on the function,
formatCheckBoxField()
;Before
After
Changed
!empty()
toisset()
, and added a check for!empty()
before looping through the array.To Reproduce
Create a custom field for a contact that is Alphanumeric and type Checkbox(es).
Attempt to save an empty value:
For those unfamiliar with using the API on the command-line, some assumptions:
id
in the input-json must be a valid Contact ID.In the above example, the custom field ID was
212
, and the contact ID was200447
.If we have a fresh-install with demo data, a likely existing contact ID, might be
99
, and a custom field ID might be something lower, such as42
.Which then would mean the command would be: