CCK text field select list options widget data incorrect when call drupal_execute() more than once

muggin - March 27, 2009 - 21:32
Project:Content Construction Kit (CCK)
Version:6.x-2.2
Component:optionwidgets.module
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

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.

<?php
/**
* 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.

#1

KarenS - March 27, 2009 - 22:35
Status:active» fixed

This is a core bug #260934: Static caching: cannot call drupal_validate_form on the same form more than once, 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.

#2

muggin - March 28, 2009 - 11:46

Thanks 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

#3

System Message - April 11, 2009 - 11:50
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.