Drupal 6.6 CCK 6.x-2.2
I am creating CCK based content programmatically using drupal_execute().
Everything works fine if only one form submission is made per HTTP request.
If more than one call to drupal_execute() is made then the issue arises.
ISSUE
====
In the code below 2 cck_widget_test content nodes are created.
The field_fuel_type CCK field is a text field with the select list option widget type enabled.
Its allowed values are petrol and diesel.
In the first generated node the resultant field_fuel_type content correctly contains 'petrol'.
In the second (and all subsequent) it contains only the single first character 'p'.
If the field_fuel_type widget type is changed from select list to text then the issue does not occur.
/**
* widget test minimal
*
*/
functionwidget_test_minimal() {
module_load_include('inc', 'node', 'node.pages');
for ($i=0; $i < 2; $i++) {
$node = array();
$node['type'] = 'cck_widget_test';
$form_state = array();
$form_state['values']['title'] = 'widget test';
$form_state['values']['field_fuel_type'][0]['value'] = 'petrol';
$form_state['values']['op'] = t('Save');
drupal_execute('cck_widget_test_node_form', $form_state, (object)$node);
}
// check for errors
$errors = form_get_errors('cck_widget_test_node_form');
if ($errors) {
dvm($errors, 'cck_widget_test form errors');
}
}
Tracing on hook_form_alter and hook_field for the first node produces:
hook_form_alter => array(1) { ["values"]=> array(3) { ["title"]=> string(11) "widget test" ["field_fuel_type"]=> array(1) { [0]=> array(1) { ["value"]=> string(6) "petrol" } } ["op"]=> string(4) "Save" } } hook_field field_fuel_type for validate => array(1) { [0]=> array(1) { ["value"]=> string(6) "petrol" } } hook_field field_fuel_type for sanitize => array(1) { [0]=> array(2) { ["value"]=> string(6) "petrol" ["safe"]=> string(6) "petrol" } }
Tracing on hook_form_alter and hook_field for the first node produces:
hook_form_alter => array(1) { ["values"]=> array(3) { ["title"]=> string(11) "widget test" ["field_fuel_type"]=> array(1) { [0]=> array(1) { ["value"]=> string(6) "petrol" } } ["op"]=> string(4) "Save" } } hook_field field_fuel_type for sanitize => array(1) { [0]=> string(6) "petrol" }
So it looks as if by the time hook_field is called for the second node things have gone astray.
Any ideas on this?
I will try some exploratory tracing in optionwidgets.module.
Comments
Comment #1
KarenS CreditAttribution: KarenS commentedThis is a core bug #260934: Static caching: when drupal_execute()ing multiple forms with same $form_id in a page request, only the first one is validated, nothing we can do about it. The recommendation is not to use drupal_execute() for multiple passes, use node_save() instead, which does not have this bug.
Comment #2
mugginsoft.net CreditAttribution: mugginsoft.net commentedThanks for directing me to this bug. All my issue searching had focussed on the single character nature of the problem.
Note that the following comment contains a functional workaround that avoids having to downgrade to node_save():
http://drupal.org/node/260934#comment-1179304