Assets field - make _getUploadedFiles publicly available / protected instead of private #11123
-
I very often have the requirement to upload files via Dropzone.js or other JS libraries in forms before the final form submission. So users can upload files to the server before the related element is uploaded. There are only a few ways to achieve this.
My wish would be to be able to upload files normally however I like, move them to a temp folder of my choice (in my custom logic) and then just hook into $files = UploadedFile::getInstancesByName($paramName);
foreach ($files as $file) {
$uploadedFiles[] = [
'filename' => $file->name,
'location' => $file->tempName,
'type' => 'custom', // use a custom type here
];
} it would even be perfect to extend your Eventually the if ($file['type'] === 'custom') {
copy($file['location'], $tempPath);
} Of course in the end of the day this is still kinda an ugly way but at least this one achieves my goal without heavy changes on your end... A better way would be to include more events or move all private functions into services so I could do $entry->on(Element::EVENT_AFTER_PROPAGATE, static function(ModelEvent $event){
$entry = $event->sender;
$field = $entry->getFieldLayout()->getFieldByHandle('image');
$targetFolderId = $field->determineUploadFolderId($entry);
// save the image by myself.. I just want that folder...
});
\Craft::$app->getElements()->saveElement($entry); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Just added a new use craft\events\LocateUploadedFilesEvent;
use craft\fields\Assets;
use yii\base\Event;
Event::on(
Assets::class,
Assets::EVENT_LOCATE_UPLOADED_FILES,
function(LocateUploadedFilesEvent $event) {
/** @var Assets $field */
$field = $event->sender;
$event->files[] = [
'type' => 'file',
'filename' => 'foo.jpg',
'path' => '/path/to/temp/foo.jpg',
];
}
); It’ll be in the next v3 and v4 releases. To get the change early, you can change your
|
Beta Was this translation helpful? Give feedback.
Just added a new
EVENT_LOCATE_UPLOADED_FILES
event to Assets fields, which you can tap into to add additional files to the mix, without needing to extendcraft\fields\Assets
.It’ll be in the next v3 and v4 releases.
To get the change early, you can ch…