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.
When submitting programmatically a node (or other entity) having a multiple file field, file_field_widget_form()
throws errors and file is not added.
This problem does not occur with single file fields.
The extra empty($form_state['programmed'])
check at line 511 is the culprit here.
Issue fork drupal-1912458
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #0.0
cameron1729 CreditAttribution: cameron1729 commentedTried to change the version number but I couldn't, so instead I explained it in the summary.
Comment #0.1
cameron1729 CreditAttribution: cameron1729 commentededited
Comment #1
cameron1729 CreditAttribution: cameron1729 commentedComment #2
bengt CreditAttribution: bengt commentedAny solution to this issue? It is the same in 7.22 as in 7.16 as far as I can tell.
Comment #3
pstewart CreditAttribution: pstewart commentedConfirmed in 7.23. This is a problem when trying to programmatically add an image that didn't previously exist, or add additional images beyond the number already saved in the field. Removing the
&& empty($form_state['programmed'])
clause of the if statement allows the additional row to be added to $elements so a single additional image can be accepted.In my case, I'm trying to add an image to an Ubercart product node via the services module. Ideally I would like to be able to add multiple additional images at once: perhaps looking at the number of submitted values in $form_state for the corresponding field and adding enough extra rows to accommodate them all?
Comment #3.0
pstewart CreditAttribution: pstewart commentedtypo
Comment #4
Jan van Diepen CreditAttribution: Jan van Diepen commentedWe need a downloadable patch for our installation profile.
So here's a patch that allows addition of an unlimited number of images to be done programmatically.
The patch is flawed so please don't use it.
Sorry I posted it in the first place.
Comment #5
davidserene CreditAttribution: davidserene commentedI uploaded and attached four images to a node from an HTML 5 application by posting the node -> posting all the images -> updating the created node for each of the images uploaded;
Comment #6
rakesh.nimje84@gmail.com CreditAttribution: rakesh.nimje84@gmail.com commentedJan van Diepen has done great job.
But he has posted the fixed value for 'field_.......'.
Here I am posting the complete code block for if condition.
Replace the complete if block with following code. Hope this will solve notice & it will be dynamic for 'field_...'
Comment #8
batje CreditAttribution: batje commentedrakesh.nimje84@gmail.com could you turn that into a patch so it is easier for people to test it?
http://drupal.org/patch/
Comment #9
leewillis77 CreditAttribution: leewillis77 commentedWe've just hit this error trying to attach an existing file to a node being created via Services. Posting the following to services would result in the warnings noted in the error log, and the node being created with no attached file.
The patch attached resolves this issue for us, but I'm not clear why the additional check was added in the first place, so could do with a robust review.
Comment #10
leewillis77 CreditAttribution: leewillis77 commentedHopefully triggering testbot.
Comment #11
mattlc CreditAttribution: mattlc commentedHi,
#9 seems to create another bug upon validation when programmatically submitted field is empty.
As "programmed" test is here to avoid "front end tricks" due to ajax I guess, i tried to skip front end things when "programmed" flag is set.
Patch attached.
Comment #13
basile.laderchi CreditAttribution: basile.laderchi commented@davidserene:
Could you post the code with what data are you posting to each service call?
I am trying to do the same thing and have no luck. (only difference is that I am creating the node and files from and Android application)
Comment #14
Shane Birley CreditAttribution: Shane Birley commentedHas anyone revisited this with any success? I am attempting to create nodes with attached files using the Services module and it works with mixed success.
Comment #16
anrikun CreditAttribution: anrikun commentedThis
empty($form_state['programmed'])
check should definitely not be here in the first place.Checking this when field is multiple but not when it is single brings behavior inconsistency:
I've turn a single file field into a multiple one and it completely broke my import function, taking me hours of debugging just for this simple check.
Comment #17
anrikun CreditAttribution: anrikun commentedComment #18
anrikun CreditAttribution: anrikun commentedComment #19
skylord CreditAttribution: skylord commentedThanks, #16 works OK and has no side effects. It's definitely a bug - single- and multiple- value fields should work in similar manner with 'programmed' forms.
Comment #20
apadernoThe following is the code after then changes done in #1059268: Files are lost when adding multiple files to multiple file fields at the same time.
The existing code was the following one.
The issue explains why that code has been added. Removing the
&& empty($form_state['programmed'])
condition will probably mean refacing the bug that issue fixed and other issues.Comment #21
apadernoDrupal 9.5 is still using that condition.
If that condition needs to be removed, it needs to first be removed in the current Drupal version.