Problem/Motivation
When a managed file form element is added to a form with multiple files allowed, once files have been added, it will display a remove selected button. The issue is that this remove button is also a submit button and the javascript triggers all elements with the .js-form-submit class. This means then when you upload another file, it triggers both the upload button and the remove button, resulting in two temporary file entities being created with the same URI. When you hit save, only one of these is marked as permanent, meaning that once the cron job runs, it deletes the temporary file, which results in the permanent filename being deleted from the system.
Steps to reproduce
1. Create a custom form element with multiple set to TRUE:
$form['exam_document'] = array(
'#type' => 'managed_file',
'#title' => t('Exam Document'),
'#required' => FALSE,
'#upload_location' => 'private://',
'#multiple' => TRUE,
'#default_value' => explode(',' , $config->get('exam_document')),
'#upload_validators' => array(
'file_validate_extensions' => array('doc docx txt pdf'),
)
);
2. Add a file to start with, wait for it to upload so that it's shown in the list below the upload element
3. Add another.
4. It'll create duplicate file records for the newer files, one marked as permanent and one as temporary.
Proposed resolution
The code needs updating to target specifically the upload button.
Remaining tasks
- Test (both manual and automated)
User interface changes
No
API changes
No
Data model changes
No