I have a (2-arity, directional) relation defined, with two fields, including a file field. I'm using the Relation Add block to create relations. The block shows the file upload widget, but after I select the file and click the upload button, I get an error:

Warning: Invalid argument supplied for foreach() in file_field_widget_submit() (line 754 of /Applications/MAMP/localhost htdocs/commonmediainc/clpp.localhost/modules/file/file.field.inc).
Warning: array_values() [function.array-values]: The argument should be an array in file_field_widget_submit() (line 761 of /Applications/MAMP/localhost htdocs/commonmediainc/clpp.localhost/modules/file/file.field.inc).
Notice: Undefined index: field_internship_report in file_ajax_upload() (line 267 of /Applications/MAMP/localhost htdocs/commonmediainc/clpp.localhost/modules/file/file.module).
Notice: Undefined index: #suffix in file_ajax_upload() (line 276 of /Applications/MAMP/localhost htdocs/commonmediainc/clpp.localhost/modules/file/file.module).

Comments

pdcarto’s picture

Also, if you click "create relation" with the file selected but not uploaded, the file does not get uploaded and the file field remains empty.

toanton’s picture

same thing with Field collection

Warning: Invalid argument supplied for foreach() in file_field_widget_submit() (line 754 of /xxx/www/modules/file/file.field.inc).
Warning: array_values() [function.array-values]: The argument should be an array in file_field_widget_submit() (line 761 of /xxx/www/modules/file/file.field.inc).

flefle’s picture

Same issue here:

Warning: Invalid argument supplied for foreach() in file_field_widget_submit() (line 754 of /home/frizi/Desktop/www/eventerior/modules/file/file.field.inc).
Warning: array_values() expects parameter 1 to be array, null given in file_field_widget_submit() (line 761 of /home/frizi/Desktop/www/eventerior/modules/file/file.field.inc).

Any suggestions?

flefle’s picture

Seems that this issue has something in common with the Fields collection module. The array structure is changed which the file upload of an image field cannot handle. Thanks for suggestions.

kpa’s picture

Same issue.

The 'similar' thing with Fields Collection at http://drupal.org/node/1329856 doesnt fix it for me.

kpa’s picture

OK, fixed the issue with a quick hack to core.

Changed this in [core]/modules/files/file.field.inc (line 754):

<?php
  $submitted_values = drupal_array_get_nested_value($form_state['values'], array_slice($button['#array_parents'], 0, -2));
  // add this line here:
  $submitted_values = (!$submitted_values) ? array() : $submitted_values;

  foreach ($submitted_values as $delta => $submitted_value) {
    [...]
?>

Very simply, makes a null return from drupal_array_get_nested_value into an empty array.

I'm pretty sure that the error is actually in drupal_array_get_nested_value() in [core]/includes/common.inc. I think it should be like this?
Edit: returning an array here causes problems elsewhere. The above code addition *should* solve the problem though.

<?php
function drupal_array_get_nested_value(array &$array, array $parents, &$key_exists = NULL) {
  $ref = &$array;

  foreach ($parents as $parent) {
    if (is_array($ref) && array_key_exists($parent, $ref)) {
      $ref = &$ref[$parent];
    }
    else {
      $key_exists = FALSE;
      return NULL; // this should be return array(); ?
    }
  }
  $key_exists = TRUE;

  return $ref;
}
?>
flefle’s picture

Thank you for your suggestion, will try them out.

flefle’s picture

kpa: the second part provokes the following error:

Warning: mb_strlen() expects parameter 1 to be string, array given in drupal_strlen() (line 441 of /home/frizi/Desktop/www/eventerior/includes/unicode.inc).

kpa’s picture

Yes it probably does.. The first part should solve your problem though?

The second part was more a suggestion that returning a blank array would solve the problem in this case, but clearly it's being called from elsewhere in the page load and requiring a NULL return.

Are you still having problems by just the first part?

mikran’s picture

Project: Relation » Relation add
Version: 7.x-1.x-dev » 7.x-0.1

I'm not sure whether field collection or relation add is better place for this but anyway moving out from relation to relation add for now.

gaëlg’s picture

#6 remove the undefined index error but I still get the ones related to file_ajax_upload(). Looks like $submitted_values should be a correct non-empty array, but it fails as described there: http://drupal.org/node/1329856#comment-5211312
Maybe $form['relation_options']['#tree'] should be TRUE for the form_state array to be built as file field want it? But I guess it will break elsewhere...

gaëlg’s picture

gaëlg’s picture

In this code (file.field.inc):

  // Go one level up in the form, to the widgets container.
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
  $field_name = $element['#field_name'];
  $langcode = $element['#language'];
  $parents = $element['#field_parents'];

  $submitted_values = drupal_array_get_nested_value($form_state['values'], array_slice($button['#array_parents'], 0, -2));
  foreach ($submitted_values as $delta => $submitted_value) {
    if (!$submitted_value['fid']) {
      unset($submitted_values[$delta]);
    }
  }

Does someone know why '#array_parents' is used to get a nested value in $form_state['values']? For me, '#array_parents' is the complete array parents list in $form variable, and '#parents' is the one for $form_state['values'], where parents are only kept if #tree is true.

I can't figure out what's wrong. Is form_state['values'] incorrectly built? Should '#array_parents' be shorter (without the first value)? Or is there a problem with file.field.inc?

gaëlg’s picture

gaëlg’s picture

Status: Active » Needs review

Adding

    '#tree' => TRUE,
    '#parents' => array('relation_options'),

to the $form['relation_options'] array fixed one part of the bug for me. This is needed if only a part of the form is given as third parameter to field_attach_form.
Now I can submit the form if I don't use the ajax button "Upload". This file_ajax_upload error is another problem, discussed in several places.

gaëlg’s picture

For the file_ajax_upload problem, it looks like replacing $form_state['values']['relation_type'] with $form_state['input']['relation_type'] in relation_add_block_form() does the trick.
This is because file_ajax_upload() calls ajax_get_form() to retrieve $form and $form_state, which populates $form_state['input'] but not $form_state['values'].

gaëlg’s picture

Here's a patch including my last two comments.

mr.york’s picture

Status: Needs review » Needs work
StatusFileSize
new1.74 KB

When we press the submit button the file wont be attached to the relation, only when the upload happens with ajax.
I've fixed some other problems in the form_submit_hook.