This seems to actually be a problem in Drupal itself. In /modules/file/file.field.inc the function file_field_widget_form has a line like this near the bottom (just before the return statement):
$elements['#file_upload_description'] = theme('file_upload_help', array('description' => '', 'upload_validators' => $elements[0]['#upload_validators']));
The issue is a small if condition a few lines back which looks like this:
if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) && empty($form_state['programmed'])) {
$elements[$delta] = $element;
...
some more stuff
}
Since I am submitting my form programatically with drupal_form_submit, it adds in $form_state[‘programmed’], thus the if condition fails, $elements[0] does not exist and I get the error
Notice: Undefined offset: 0 in file_field_widget_form()
My form is submitted by a custom routine (in a custom module) that looks a bit like this:
foreach ($field_data as $k => $detail) {
$field_key = _get_field_keys('field_' . $k);
$field_key = $field_key[0];
$node -> {'field_' . $k}[$node -> language][0][$field_key] = $detail;
$form_state['values']['field_' . $k] = $detail;
}
drupal_form_submit("{$node->type}_node_form", $form_state, $node);
The form state before I call drupal_form_submit looks like this:
Array
(
[values] => Array
(
[title] => some title
[name] => admin
[op] => Save
[field_person] => 88
[field_lead_type] => 1
[field_lead_details] => some stuff
)
)
What is the fix? I’d rather not mess with core modules. Is there something I can do to my own code to resolve the problem?