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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

arne_hortell’s picture

Hi,

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']));

yched’s picture

Component: field system » file.module

Recategorizing

Altcom_Alan’s picture

This 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.

musicalvegan0’s picture

I can confirm that this is still an issue with Drupal 7.23:

Notice: Undefined offset: 0 in file_field_widget_form() (line 518 of /var/www/html/devel/modules/file/file.field.inc).
Notice: Undefined offset: 0 in image_field_widget_form() (line 358 of /var/www/html/devel/modules/image/image.field.inc).

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.

mccrodp’s picture

Issue summary: View changes

On 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?

"field_image": {
		"und": [
		  {
			"fid": "54"
		  },
		  {
			"fid": "55"
		  }
		]
	}

EDIT: Sorry, this is most likely due to a related issue with Services module.

tkuldeep17’s picture

Status: Active » Needs review
DigitalFrontiersMedia’s picture

kaare’s picture

Does 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'].

isinadinos’s picture

I 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?