Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Ability to use files fieldtype in forms for attaching temporary files #9084

Merged

Conversation

duncanmcclean
Copy link
Member

@duncanmcclean duncanmcclean commented Nov 29, 2023

This pull request allows you to use the Files fieldtype in forms, enabling you to have file uploads which get deleted from the server after the form emails have sent.

This PR also makes a small improvement to the handling of assets in form submission emails. When the "Attachments" toggle is enabled on an email, we'll now filter out any Asset fields when displaying the submission data.

We figured that there's not much point in displaying the asset URLs if they're already attached to the email.


Closes #7711.

@duncanmcclean duncanmcclean changed the title [4.x] Add option to delete attachment files after form submission [4.x] Add option to delete assets after sending form emails Nov 29, 2023
@duncanmcclean duncanmcclean marked this pull request as ready for review November 29, 2023 18:18
Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was really well done, nice work. Sorry for the delay.

I think it's a of an awkward flow to have to upload and create assets only for them to be deleted.

We have a files fieldtype that works similar to assets and puts the files in a temporary location, leaving it there for you to do whatever you want. We use this in the "Reupload Asset" action.

I think instead of a "delete after upload" setting, we could let people use the files fieldtype to be able to upload files that will only be used as attachments. Then in the job we can delete those files.

Also if you want to you can target 5.x which might make tests and composer dependencies a bit simpler.

@duncanmcclean duncanmcclean marked this pull request as draft March 25, 2024 17:31
@duncanmcclean duncanmcclean changed the title [4.x] Add option to delete assets after sending form emails [5.x] Add option to delete assets after sending form emails Mar 25, 2024
@duncanmcclean duncanmcclean changed the base branch from 4.x to master March 25, 2024 17:31
duncanmcclean and others added 10 commits March 25, 2024 17:56
…sion' of github.com:statamic/cms into add-option-to-delete-attachment-files-after-form-submission
Laravel 10.33 includes this PR which we're using to test the form email jobs are being queued/chained succesfully.

laravel/framework#49003
@duncanmcclean duncanmcclean marked this pull request as ready for review March 26, 2024 15:01
@duncanmcclean
Copy link
Member Author

We have a files fieldtype that works similar to assets and puts the files in a temporary location, leaving it there for you to do whatever you want. We use this in the "Reupload Asset" action.

I think instead of a "delete after upload" setting, we could let people use the files fieldtype to be able to upload files that will only be used as attachments. Then in the job we can delete those files.

Also if you want to you can target 5.x which might make tests and composer dependencies a bit simpler.

Good idea, I've re-worked this PR around the "Files" fieldtype instead. One less setting 😅

I've also re-targeted this PR at master.

$fields = $this->getRenderableFieldData(Arr::except($augmented, ['id', 'date', 'form']));

if (array_has($this->config, 'attachments')) {
$fields = $fields->reject(fn ($field) => $field['fieldtype'] === 'assets');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do asset fields need to get rejected?

Copy link
Member Author

@duncanmcclean duncanmcclean Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the "Attachments" toggle is enabled on a form email, this code hides the Assets fields from being displayed in the form data since the Asset is going to be attached anyway.

Otherwise, it'd show a URL to the asset (which when we were using Assets fields for uploads would 404 anyway).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, that's made me realise we should be filtering out Files fields here too.

duncanmcclean and others added 6 commits March 26, 2024 15:52
- arrow closures etc
- use Collection::wrap() which is identical to collect(Arr::wrap())
- use submission remove instead of set null
- save once at the end rather than in each iteration
@jasonvarga jasonvarga changed the title [5.x] Add option to delete assets after sending form emails [5.x] Ability to use files fieldtype in forms for attaching temporary files Mar 28, 2024
@jasonvarga jasonvarga merged commit 1157fd7 into master Mar 28, 2024
32 checks passed
@jasonvarga jasonvarga deleted the add-option-to-delete-attachment-files-after-form-submission branch March 28, 2024 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A form without "save answers" still saves the file uploads
2 participants