diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index c1834d7b6..cbd4c8410 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -1,9 +1,11 @@ previewMode = true; } + $this->getConfigFormWidget(); $this->checkUploadPostback(); } @@ -142,6 +151,44 @@ class FileUpload extends FormWidgetBase $this->vars['prompt'] = $this->getPromptText(); } + /** + * Get the file record for this request, returns false if none available + * + * @return System\Models\File|false + */ + protected function getFileRecord() + { + $record = false; + + if (!empty(post('file_id'))) { + $record = $this->getRelationModel()::find(post('file_id')) ?: false; + } + + return $record; + } + + /** + * Get the instantiated config Form widget + * + * @return void + */ + public function getConfigFormWidget() + { + if ($this->configFormWidget) { + return $this->configFormWidget; + } + + $config = $this->makeConfig('~/modules/system/models/file/fields.yaml'); + $config->model = $this->getFileRecord() ?: $this->getRelationModel(); + $config->alias = $this->alias . $this->defaultAlias; + $config->arrayName = $this->getFieldName(); + + $widget = $this->makeWidget(Form::class, $config); + $widget->bindToController(); + + return $this->configFormWidget = $widget; + } + protected function getFileList() { $list = $this @@ -302,7 +349,7 @@ class FileUpload extends FormWidgetBase public function onLoadAttachmentConfig() { $fileModel = $this->getRelationModel(); - if (($fileId = post('file_id')) && ($file = $fileModel::find($fileId))) { + if ($file = $this->getFileRecord()) { $file = $this->decorateFileAttributes($file); $this->vars['file'] = $file; @@ -324,10 +371,15 @@ class FileUpload extends FormWidgetBase { try { $fileModel = $this->getRelationModel(); - if (($fileId = post('file_id')) && ($file = $fileModel::find($fileId))) { - $file->title = post('title'); - $file->description = post('description'); - $file->save(); + if ($file = $this->getFileRecord()) { + $formWidget = $this->getConfigFormWidget(); + + $modelsToSave = $this->prepareModelsToSave($file, $formWidget->getSaveData()); + Db::transaction(function () use ($modelsToSave, $formWidget) { + foreach ($modelsToSave as $modelToSave) { + $modelToSave->save(null, $formWidget->getSessionKey()); + } + }); return ['displayName' => $file->title ?: $file->file_name]; } diff --git a/modules/backend/formwidgets/fileupload/partials/_config_form.htm b/modules/backend/formwidgets/fileupload/partials/_config_form.htm index 621c51650..91302a479 100644 --- a/modules/backend/formwidgets/fileupload/partials/_config_form.htm +++ b/modules/backend/formwidgets/fileupload/partials/_config_form.htm @@ -23,22 +23,7 @@
= e(trans('backend::lang.fileupload.help')) ?>
-