This simple test form triggers the following Fatal error on submit (make sure to upload a file...):
"Cannot use object of type stdClass as array in /var/www/drupal/includes/form.inc on line 1320"
Without the "#tree = true" it works fine.
function tst_form(&$form_state) {
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#tree'] = true; // remove me...
$form['image'] = array(
'#type' => 'image_upload_element',
'#title' => 'img',
'#required' => true,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
Comments
Comment #1
alan d. commentedIt looks like this is a possible issue with saving objects as element values in Drupal, which was the primary philosophy behind the module.
Casting the file object to an array bypasses this error in "form.inc", but breaks the module as it is expecting an object not an array.
If this is the case, then the module will have to be completely rewritten, a task that I simply do not have time to do at the moment.
I will update the intro to note this issue and leave this issue open in case some FAPI genius can come up with a simple workaround.
Otherwise it will have to wait till the next major release (6.x-2.0), as the changes will change the basic interface of the module. I am also planning multiple file support, so it is going to be a fairly major job for the next release.
Comment #2
edgar83 commentedMy solution is to put #tree = false on the upload element in my form. The form stays nested, except for this specific element.
Thanks for your great module btw :-)
Comment #3
alan d. commentedThanks for the feedback / workaround.
I'll leave this issue open for a future releases / versions.
Comment #4
Electronick commentedWe can use simple (object)$array and (array)$object conversion. But don't know where.
Comment #5
alan d. commentedThe solution is not as simple as this. Casting all of the objects into arrays cascaded into another series of bugs. This should be addressed later when things get refactored a bit.
Comment #6
jaypanThis is still an issue - I just dealt with it.
I believe that the problem in the way that the upload element renders the HTML ID attribute of the elements. When items are set to #tree, the ID gets square parentheses ([ and ]) inserted into ID. This is invalid HTML. I went in and changed upload_element.module myself and hardcoded an ID for testing purposes, and the element began to work again. Of course, this isn't something I want to depend on.
What I ended up doing was removing #tree, and instead naming my elements like this:
I had to add in a little regex on the processing end to deal with this however. This adds an extra load on the server, so it's by no means an optimal solution. However, this is the only way I could think to take care of the issue without having to alter the original module code.
Comment #7
madmanmax commentedYou cannot set
#tree = TRUEand usefile_save_upload. See this thread: #83698: $source parameter in file_save_upload() not work as expected when field is in a fieldset with #tree set to TRUEI had a hard time with this when making my own upload fields. Comment #6 is the only workaround I found so far. I'm not sure if you need regex for this though.
Comment #8
alan d. commentedClosed because Drupal 6 is no longer supported.