@@ -60,6 +60,7 @@ private function registerSubfieldEvent(array $subfield, bool $registerModelEvent
6060 $ uploader = $ this ->getUploader ($ subfield , $ this ->uploaderConfiguration );
6161 $ crudObject = $ this ->crudObject ->getAttributes ();
6262 $ uploader = $ uploader ->repeats ($ crudObject ['name ' ]);
63+ $ uploader = $ uploader ->fake ((isset ($ crudObject ['fake ' ]) && $ crudObject ['fake ' ]) ? ($ crudObject ['store_in ' ] ?? 'extras ' ) : false );
6364
6465 // If this uploader is already registered bail out. We may endup here multiple times when doing modifications to the crud object.
6566 // Changing `subfields` properties will call the macros again. We prevent duplicate entries by checking
@@ -139,6 +140,14 @@ private function setupModelEvents(string $model, UploaderInterface $uploader): v
139140 $ uploader ->deleteUploadedFiles ($ entry );
140141 });
141142
143+ // if the uploader is a relationship and handles repeatable files, we will also register the deleting event on the
144+ // parent model. that way we can control the deletion of the files when the parent model is deleted.
145+ if ($ uploader ->isRelationship () && $ uploader ->handleRepeatableFiles ) {
146+ app ('crud ' )->model ::deleting (function ($ entry ) use ($ uploader ) {
147+ $ uploader ->deleteUploadedFiles ($ entry );
148+ });
149+ }
150+
142151 app ('UploadersRepository ' )->markAsHandled ($ uploader ->getIdentifier ());
143152 }
144153
@@ -154,9 +163,13 @@ private function setupModelEvents(string $model, UploaderInterface $uploader): v
154163 */
155164 private function getUploader (array $ crudObject , array $ uploaderConfiguration ): UploaderInterface
156165 {
157- $ customUploader = isset ($ uploaderConfiguration ['uploader ' ]) && class_exists ($ uploaderConfiguration ['uploader ' ]);
166+ $ hasCustomUploader = isset ($ uploaderConfiguration ['uploader ' ]);
167+
168+ if ($ hasCustomUploader && ! is_a ($ uploaderConfiguration ['uploader ' ], UploaderInterface::class, true )) {
169+ throw new Exception ('Invalid uploader class provided for ' .$ this ->crudObjectType .' type: ' .$ crudObject ['type ' ]);
170+ }
158171
159- if ($ customUploader ) {
172+ if ($ hasCustomUploader ) {
160173 return $ uploaderConfiguration ['uploader ' ]::for ($ crudObject , $ uploaderConfiguration );
161174 }
162175
0 commit comments