Problem/Motivation

For my project, I need the ability to upload a file in a repeatable custom composite.

The composite consists of two fields, a single taxonomy term reference and a single file upload.

The only feature that is needed for the file upload is the ability to limit/define allowed file types.

Webform composite elements do not support managed file uploads.

Proposed resolution

The managed file element requires custom save, update, and delete callbacks which are used to rename, track, and managed upload files.

Rework the ManagedFile element to provide callbacks that can be re-used by Webform composite elements

Remaining tasks

  • Allow managed files to be used with composite elements.
  • Setup test composite managed file upload form with single and multiple examples.
  • Determine where a composite element can save and delete managed files.
  • Update WebformManagedFileBase::postSave and add ::syncFiles() and ::updateFiles() helper methods.
  • Add file upload to webform_example_composite.module.
  • Write tests.
  • Write a change record.

User interface changes

Add ManagedFile elements to support composite element drop down.

API changes

  • ::postSave and ::postDelete methods need to be added to the WebformCompositeElement plugin.
  • WebformManagedFileBase::deleteFiles will need to be public and maybe static

Data model changes

N/A

Comments

brado created an issue. See original summary.

jrockowitz’s picture

If all you immediately need is to limit the file upload types. I think you are missing a simple property. Can you please post your custom composite code in a zipped module?

brado’s picture

StatusFileSize
new8.58 KB

Here's what I have so far.

The file upload field loads but it doesn't work. I was saying the only feature I need from he field (other than uploading the file) is the ability to limit file types. I assumed it was the attribute that I was setting but wanted to make sure all the details were in there.

jrockowitz’s picture

StatusFileSize
new1.04 KB

@brado You need to use '#type': managed_file instead of '#type': file.

The attached patch removes the exception which disables support for composite elements with a managed file upload element.

Your custom composite element starts to work but upload file is not being properly saved (ie it is still a temp file)

We need to rework the \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase and the below methods would need to be refactored.

\Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::postSave
\Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::postDelete
\Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::getFileDestinationUri
\Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::getTestValues

The goal would be to get managed file uploads working for custom composites via the UI and code.

There is decent test coverage for the existing managed file element but we would need to write some more tests to prevent any regressions.

I can fully scope out this work over the weekend.

brado’s picture

@jrock Awesome. You are a life saver!

jrockowitz’s picture

Issue summary: View changes
jrockowitz’s picture

Issue summary: View changes

  • jrockowitz committed 9b18654 on 3007890-file-upload
    Issue #3007890 by jrockowitz, brado: File Upload field in repeatable...

  • jrockowitz committed 5e8cd62 on 3007890-file-upload
    Issue #3007890 by jrockowitz, brado: File Upload field in repeatable...

  • jrockowitz committed a47ac2c on 3007890-file-upload
    Issue #3007890 by jrockowitz, brado: File Upload field in repeatable...
  • jrockowitz committed ff2cd00 on 3007890-file-upload
    Issue #3007890 by jrockowitz, brado: File Upload field in repeatable...
jrockowitz’s picture

Status: Active » Needs review
StatusFileSize
new48.2 KB

Please review the attached patch. Make sure the change 'file' to 'managed_file' in your custom composite.

Status: Needs review » Needs work

The last submitted patch, 11: 3007890-11.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • jrockowitz committed 3d575b5 on 3007890-file-upload
    Issue #3007890 by jrockowitz, brado: File Upload field in repeatable...
  • jrockowitz committed a7a73a1 on 3007890-file-upload
    Issue #3007890 by jrockowitz, brado: File Upload field in repeatable...
  • jrockowitz committed c49647b on 3007890-file-upload
    Issue #3007890 by jrockowitz, brado: File Upload field in repeatable...
jrockowitz’s picture

Status: Needs work » Needs review
StatusFileSize
new48.31 KB
brado’s picture

Works perfect!

Confirmed the file upload template side works like a charm and it shows up in the Webfrom REST GET response just like it needs to.

jrockowitz’s picture

Here is the change record.

  • jrockowitz committed bfbabbe on 8.x-5.x
    Issue #3007890 by jrockowitz, brado: File Upload field in repeatable...
jrockowitz’s picture

Status: Needs review » Fixed

Done!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.