@@ -97,6 +97,13 @@ class Field extends OrganicField implements JsonSerializable
9797 */
9898 protected $ defaultCallback ;
9999
100+ /**
101+ * Closure be used for the field's default value when store/update.
102+ *
103+ * @var callable
104+ */
105+ protected $ appendCallback ;
106+
100107 /**
101108 * Closure be used to be called after the field value stored.
102109 */
@@ -190,12 +197,22 @@ public function fillAttribute(RestifyRequest $request, $model)
190197 {
191198 $ this ->resolveValueBeforeUpdate ($ request , $ model );
192199
193- if (isset ($ this ->fillCallback )) {
200+ if ($ this ->isHidden ($ request )) {
201+ if (! isset ($ this ->appendCallback )) {
202+ return ;
203+ }
204+ }
205+
206+ if (! $ this ->isHidden ($ request ) && isset ($ this ->fillCallback )) {
194207 return call_user_func (
195208 $ this ->fillCallback , $ request , $ model , $ this ->attribute
196209 );
197210 }
198211
212+ if (isset ($ this ->appendCallback )) {
213+ return $ this ->fillAttributeFromAppend ($ request , $ model , $ this ->attribute );
214+ }
215+
199216 if ($ request ->isStoreRequest () && is_callable ($ this ->storeCallback )) {
200217 return call_user_func (
201218 $ this ->storeCallback , $ request , $ model , $ this ->attribute
@@ -227,6 +244,28 @@ protected function fillAttributeFromRequest(RestifyRequest $request, $model, $at
227244 }
228245 }
229246
247+ /**
248+ * Fill the model with the default value.
249+ *
250+ * @param RestifyRequest $request
251+ * @param $model
252+ * @param $attribute
253+ */
254+ protected function fillAttributeFromAppend (RestifyRequest $ request , $ model , $ attribute )
255+ {
256+ if (! isset ($ this ->appendCallback )) {
257+ return ;
258+ }
259+
260+ if (is_callable ($ this ->appendCallback )) {
261+ return $ model ->{$ attribute } = call_user_func (
262+ $ this ->appendCallback , $ request , $ model , $ attribute
263+ );
264+ }
265+
266+ $ model ->{$ attribute } = $ this ->appendCallback ;
267+ }
268+
230269 /**
231270 * @return callable|string|null
232271 */
@@ -477,4 +516,33 @@ public function invokeAfter(RestifyRequest $request, $repository)
477516 call_user_func ($ this ->afterUpdateCallback , $ this ->resolveAttribute ($ repository , $ this ->attribute ), $ this ->valueBeforeUpdate , $ repository , $ request );
478517 }
479518 }
519+
520+ /**
521+ * Indicate whatever the input is hidden or not.
522+ *
523+ * @param bool $callback
524+ * @return $this
525+ */
526+ public function hidden ($ callback = true )
527+ {
528+ $ this ->hideFromIndex ($ callback )
529+ ->hideFromShow ($ callback );
530+
531+ $ this ->hiddenCallback = $ callback ;
532+
533+ return $ this ;
534+ }
535+
536+ /**
537+ * Append values when store/update.
538+ *
539+ * @param callable|string $value
540+ * @return $this
541+ */
542+ public function append ($ value )
543+ {
544+ $ this ->appendCallback = $ value ;
545+
546+ return $ this ;
547+ }
480548}
0 commit comments