Hello,
We have been having some trouble getting check-box (boolean) fields in Salesforce to properly import to check box CCK values in Drupal. It looks like other issues have touched on the handling of CCK boolean values (such as #506032: CCK checkbox groups do not sync correctly and #1195824: Incorrect massage of 'false' boolean values), though from what I can gather they don't fully address the import situation we have encountered (and which I imagine could be rather common).
In our case we have check-box values in SF importing to a check-box CCK values in Drupal. So Salesforce is sending either (bool) TRUE or FALSE and Drupal is "expecting" (for proper display handling) either an "on" or "off" key value as defined in the fields "allowed values" configuration. However, what Drupal ultimately gets is a 1 if SF sends TRUE and a NULL if SF sends FALSE.
From what I can gather, the problem lies in the default import handler for cck check-boxes, which ends up being _sf_node_import_cck_default. It looks like an export handler for check-boxes has been nicely refined in #1195824: Incorrect massage of 'false' boolean values, but it does not look like an appropriate import handler is yet available for check-boxes (unless I am missing something).
I'm playing around with a custom check-box import handler to see if it can lead to the correct behavior in this case, and will post that here shortly.
Comments
Comment #1
rjacobs commentedOk, I have been seeing good results with the following import handler:
Which I'm enabling (for CCK fields that use the "optionwidgets_onoff" widget) in a custom module with hook_fieldmap_objects_alter:
If this import functionality is not already being managed elsewhere, perhaps a handler of this nature should be included in sf_node or sf_contrib? If so I could roll this as a patch.
Comment #2
mtbosworth commentedI found the same issue with salesforce module failing to transfer checkbox data. I am writing simpletests for my field maps and am noticing that on all checkboxes. I've added your code to my site controller module, but its not fixing my issue. I am using integer datatype for my checkboxes.
Comment #3
rjacobs commentedHi mtbosworth,
So you are using 0 and 1 as the keys in Drupal for a checkbox? Something like:
If so, I believe you have run into the same issue I noted in http://drupal.org/node/1195824#comment-5595962. Though that comment in issue #1195824 relates to exporting, the same on/off value calculations are used in the code I posed above. When commenting on #1195824 I also adjusted my test code for this import logic, but didn't post the change back to this thread.
So, to make a long story short, you might just want to replace the first chunk of code above with:
The only change is that the line:
array_shift($allowed_values);became:
next($allowed_values);Hope that helps a bit.... let me know if you have any luck.
Comment #4
rjacobs commentedAlso, given that you are using an integer datatype for checkboxes I wonder if the CCK field still uses the 'optionwidgets_onoff' widget type. If not, my code example won't detect that case and won't set _sf_node_import_cck_checkbox as the import handler. I'd be curious to know if you are able to confirm this with a bit of testing. Maybe throw a $dpm($objects); check at the end of function module_fieldmap_objects_alter(&$objects) and see if the import handler is infact set as _sf_node_import_cck_checkbox for your checkboxes?
Comment #5
mtbosworth commentedI ran this code through my automated tests and all the check boxes passed. That's great. Thanks for the for code. Is hook_fieldmap_objects_alter run after hook_pre_import and before hook_pre_export? I have other field issues to fix involving dates.
Comment #6
kostajh commentedComment #7
rjacobs commentedShould this better be labeled as a feature request? I just wanted to inquire about why it was closed.
Comment #8
kostajh commentedThis is why it was closed: https://drupal.org/node/1283350#comment-6831112
If someone submits a patch, and it is tested by another person, it can be committed.
Comment #9
rjacobs commentedGot it. I noted in the initial message that I'd be willing to roll this as a patch, but given that this code can also easily be deployed in a custom (case-specific) module as an implementation of hook_fieldmap_objects_alter let's just leave it as-is. Hopefully the notes here will help someone who may need this. I'm just hoping/assuming this is not an issue in the new 7.x-3.x branch.
Comment #10
rjacobs commented