Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
If you try to submit a form with a file field that has file_widget_multiple set programmatically, you get the following error: Notice: Undefined offset: 0 in file_field_widget_form() (regel 514 van /home/adrupal/www/modules/file/file.field.inc).
This is due to the fact that file_field_widget_form expects there to be at least one row/element, but in case of a programmatic form submission this is not true. To allow for programmatic form submissions of, for example, node edits, it is necessary that this problem be fixed.
Comment | File | Size | Author |
---|---|---|---|
#4 | 1434312-file.field_.patch | 1.2 KB | musicalvegan0 |
#4 | 1434312-image.field_.patch | 1.03 KB | musicalvegan0 |
Comments
Comment #1
arne_hortell CreditAttribution: arne_hortell commentedHi,
I changed this (514)
$elements['#file_upload_description'] = theme('file_upload_help',array( 'description' => '','upload_validators' => $elements[0]['#upload_validators']));
To this
// To prevent error when form_submitted programatically
if (array_key_exists(0,$elements))
$elements['#file_upload_description'] = theme('file_upload_help',
array( 'description' => '',
'upload_validators' => $elements[0]['#upload_validators']));
Comment #2
yched CreditAttribution: yched commentedRecategorizing
Comment #3
Altcom_Alan CreditAttribution: Altcom_Alan commentedThis problem also cascades into image_field_widget_form() in image.field.inc line 358 which basically has the same line of code:
$elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators']));
This generates the same warning when submitting a form programatically, so it seems to me the fix should be to remove the check for a programmed form from the code that adds the empty row in line 488 which will mean there will allways be a $elements[0]:
if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) && empty($form_state['programmed'])) {
becomes:
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) {
I've not yet tested but I don't see why the widget would do anything strange when it's a programmed form if there is an empty row.
Comment #4
musicalvegan0 CreditAttribution: musicalvegan0 commentedI can confirm that this is still an issue with Drupal 7.23:
I can also confirm that #1434312-1: file_widget_multiple doesn't allow programmatic form submission solves the issue.
Submitting patches against current 7.x.
Comment #5
mccrodp CreditAttribution: mccrodp commentedOn Drupal 7.27 this patch did remove the PHP Notice, but it did not assign the files to the indexes correctly. When the image field's "Number of values" is set to 1 the 1st field below as part of the JSON node to be imported works correctly and the existing image on the server is assigned to the field in Drupal. When the image field's "Number of values" is set to greater than 1, neither image is associated.
No errors or anything suspicious found in the logs: admin/reports/dblog
Any ideas? Are we sure this works in Drupal 7.27?
EDIT: Sorry, this is most likely due to a related issue with Services module.
Comment #6
tkuldeep17 CreditAttribution: tkuldeep17 commentedComment #7
DigitalFrontiersMediaIs this the same problem as #1912458: file_field_widget_form() broken when submitting programmatically a form having a multiple file field?
Comment #8
kaareDoes anyone have a cool workaround for this without hacking/patching core? Otherwise, after a quick inspection of the code in question, I think the solution in #3 is on the right path. Either remove the test for whether the form is programmed, or add an additional test for whether it is programmed during the manipulating of
$elements['#file_upload_description']
.Comment #9
isinadinos CreditAttribution: isinadinos commentedI patched core with #4 but it didn't work. Is this code applicable for services 3.14 version?
I upload the image first and i got response fid. Then I post the following json but I can't see the image in the node.
{
"title": "Report Problem",
"type": "report",
"language": "und",
"uid":"1",
"body" : {
"und": [
{
"value": "test body"
} ]
},
"field_tags": { "und": "2" },
"field_image": {"und": [{"fid":"13"}]}
}
Any suggestions?