When you use drupal_execute to submit a $form_state with a optionwidget_onoff checkbox unchecked the widget will not recognize the value as 'off' since Drupal reports a boolean FALSE for the checkbox when using drupal_execute while the widget expects strictly (===) integer 0.
To reproduce:
Create a new integer or text (type doesnt seem to matter) field with optionwidget_onoff as widget
Supply allowed values:
0|Unchecked
1|Checked
of
"foo"|Unchecked
"bar"|Checked
in case of Text field, again this can probably be anything
Fill out sometype_node_form using drupal_execute
$form_state['values']['field_zzz'][0]['values'] = 0;
drupal_execute('sometype_node_form', $form_state, $node_obj);
field_zzz will stay "NULL" after drupal_execute submits the form.
Patch, please excuse the mercurial diff on my local repo
--- a/httproot/sites/all/modules/cck/modules/optionwidgets/optionwidgets.moduleFri Oct 02 19:02:45 2009 -0400
+++ b/httproot/sites/all/modules/cck/modules/optionwidgets/optionwidgets.moduleFri Oct 02 21:12:27 2009 -0400
@@ -371,7 +371,7 @@
$values = array_values($items);
- if ($element['#type'] == 'optionwidgets_onoff' && ($values[0] === 0)) {
+ if ($element['#type'] == 'optionwidgets_onoff' && ($values[0] == 0)) {
$keys = array_keys($options);
$values = array(array_key_exists(0, $keys) ? $keys[0] : NULL);
}
$values[0] is 0 and type int in case of normal form submit
$values[0] is FALSE and type boolean in case of drupal_execute submit
The widget should allow for both values since semantically they mean the same thing for checkboxes
Comment | File | Size | Author |
---|---|---|---|
#3 | 594688-optionwidgets_data2form-drupal_execute.patch | 695 bytes | Matthew Davidson |
Comments
Comment #1
nick.dap CreditAttribution: nick.dap commentedComment #2
nick.dap CreditAttribution: nick.dap commentedConfirmed still an issue in 6.x-2.5
Comment #3
Matthew Davidson CreditAttribution: Matthew Davidson commentedThis issue seems related to others (#374558: Multi-value Checkbox fields break drupal_execute()) which have been closed I think unjustifiably. It's fine to say "You're not using the correct format of the field in the form state values array," but one should expect that loading an existing node into a form that you drupal_execute() should Just Work. The problem I had was that in my module at least, I was getting "An illegal choice has been detected. Please contact the site administrator." instead.
The attached patch for 6.x-2.9, following the same rationale as the above suggestion, addresses this issue with no side-effects for manual form submission that I can detect.
Comment #4
roderikI've never used drupal_execute but just solved a problem where 'off' values were not being saved. (In my case, when #access was set to FALSE on an form element.)
It approaches things from the other side: instead of changing optionwidgets_data2form(), it takes care that optionwidgets_data2form() always gets passed an integer 0 for the 'off' value.
(Plus I can think up the theory behind it - in the comments of that patch.)
From reading the code above, I think #716408-19: Single on/off checkbox: hook_form_alter() with ['#access'] = FALSE changes 'On' value to TRUE might also solve your problems. Worth a test?