From 0086a47b806082d07b3b03c8ff8df5f58c961639 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Mon, 4 Jun 2018 16:22:23 -0600 Subject: [PATCH] Add attachOnUpload option to FileUpload formwidget Fixes #3501 --- modules/backend/formwidgets/FileUpload.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index cef13c9c2..006ba8cfd 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -71,6 +71,11 @@ class FileUpload extends FormWidgetBase */ public $useCaption = true; + /** + * @var boolean Automatically attaches the uploaded file on upload if the parent record exists instead of using deferred binding to attach on save of the parent record. Defaults to false. + */ + public $attachOnUpload = false; + // // Object properties // @@ -92,7 +97,8 @@ class FileUpload extends FormWidgetBase 'fileTypes', 'mimeTypes', 'thumbOptions', - 'useCaption' + 'useCaption', + 'attachOnUpload', ]); if ($this->formField->disabled) { @@ -395,10 +401,11 @@ class FileUpload extends FormWidgetBase $file->save(); /** - * Attach directly to the parent model if it exists, + * Attach directly to the parent model if it exists and attachOnUpload has been set to true * else attach via deferred binding */ - if (@$fileRelation->getParent()->exists) { + $parent = $fileRelation->getParent(); + if ($this->attachOnUpload && $parent && $parent->exists) { $fileRelation->add($file); } else { $fileRelation->add($file, $this->sessionKey);