@@ -115,6 +115,8 @@ class Field extends OrganicField implements JsonSerializable
115
115
*/
116
116
protected $ valueCallback ;
117
117
118
+ protected $ fillDefaultCallback ;
119
+
118
120
/**
119
121
* Closure be used to be called after the field value stored.
120
122
*/
@@ -205,6 +207,22 @@ public function fillCallback(callable|Closure $callback)
205
207
return $ this ;
206
208
}
207
209
210
+ /**
211
+ * This is called after the fill and store callbacks.
212
+ *
213
+ * You can pass a callable or a value, and it will be attached to the model if no value provided otherwise.
214
+ *
215
+ * Imagine it's like `attributes` in the model.
216
+ *
217
+ * @return $this
218
+ */
219
+ public function defaultCallback (mixed $ callback )
220
+ {
221
+ $ this ->defaultCallback = $ callback ;
222
+
223
+ return $ this ;
224
+ }
225
+
208
226
/**
209
227
* Fill attribute with value from the request or delegate this action to the user defined callback.
210
228
*
@@ -246,6 +264,12 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n
246
264
$ bulkRow
247
265
);
248
266
267
+ $ this ->fillAttributeFromDefault (
268
+ $ request ,
269
+ $ model ,
270
+ $ this ->label ?? $ this ->attribute
271
+ );
272
+
249
273
$ this ->fillAttributeFromValue (
250
274
$ request ,
251
275
$ model ,
@@ -310,6 +334,23 @@ protected function fillAttributeFromValue(RestifyRequest $request, $model, $attr
310
334
return $ this ;
311
335
}
312
336
337
+ protected function fillAttributeFromDefault (RestifyRequest $ request , $ model , $ attribute )
338
+ {
339
+ if ($ model ->{$ attribute }) {
340
+ return $ this ;
341
+ }
342
+
343
+ if (! isset ($ this ->fillDefaultCallback )) {
344
+ return $ this ;
345
+ }
346
+
347
+ $ model ->{$ attribute } = is_callable ($ this ->fillDefaultCallback )
348
+ ? call_user_func ($ this ->fillDefaultCallback , $ request , $ model , $ attribute )
349
+ : $ this ->fillDefaultCallback ;
350
+
351
+ return $ this ;
352
+ }
353
+
313
354
/**
314
355
* @return callable|string|null
315
356
*/
0 commit comments