-
-
Notifications
You must be signed in to change notification settings - Fork 607
Labels
Description
Bug Description
I have a form with a file upload. After uploading the form it uses the SubmissionCreated event to rename the file. But after calling $asset->rename() the meta-yaml-file loses properties.
Screenshot-from-2021-06-16-13-18-42.png.yaml
data: { }
size: 6294
last_modified: 1624437139
width: 620
height: 84
mime_type: image/pngDoe_John_Screenshot-from-2021-06-16-13-18-42.png.yaml
data: { }When viewing the form submission in the CP the meta is recreated.
How to Reproduce
Easy way:
Probably just add an asset and call rename() on it
The way in my project:
- Create a form with fields firstname, lastname and file
- Create app/Listeners/RenameFormAssets.php
- Insert code from "Extra Detail" below
- Enable event discovery: https://laravel.com/docs/8.x/events#event-discovery
- Submit the form
Extra Detail
app/Listeners/RenameFormAssets.php
<?php
namespace App\Listeners;
use Statamic\Facades\Asset;
use Statamic\Events\SubmissionCreated;
class RenameFormAssets
{
/**
* Handle the event.
*
* @param SubmissionCreated $event
* @return void
*/
public function handle(SubmissionCreated $event)
{
$submission = $event->submission;
$form = $submission->form();
$fileField = $form->blueprint()->fields()->get('file');
$assetContainer = $fileField->config()['container'];
$asset = Asset::find($assetContainer . '::' . $submission->get('file'));
$newFileName = implode('_', [
$submission->get('lastname'),
$submission->get('firstname'),
$asset->filename(),
]);
$asset->rename($newFileName);
$submission->set('file', $asset->path());
$submission->save();
}
}Environment
Statamic version: 3.1.26
PHP version: 7.4
Install method (choose one):
- Fresh install from
statamic/statamic