Skip to content

Commit b15e3f6

Browse files
authored
9.6.0 (#619)
* feat: support string url as path for files * Fix styling * fix: fixing str * feat: add defaultCallback method * Fix styling * fix: docs * Fix styling --------- Co-authored-by: binaryk <binaryk@users.noreply.github.com>
1 parent a2f8aaa commit b15e3f6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

docs-v2/content/en/api/fields.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ Now, for the fields that don't have a description into the database, it will ret
369369
The default value is ONLY used for the READ, not for WRITE requests.
370370
</alert>
371371

372+
### Default Stored Value
373+
374+
During any (update or store requests), this is called after the fill and store callbacks.
375+
376+
You can pass a callable or a value, and it will be attached to the model if no value provided otherwise.
377+
378+
Imagine it's like `attributes` in the model:
379+
380+
```php
381+
field('currency')->defaultCallback('EUR'),
382+
```
383+
372384
## Customizations
373385

374386
### Field label

src/Fields/Field.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class Field extends OrganicField implements JsonSerializable
115115
*/
116116
protected $valueCallback;
117117

118+
protected $fillDefaultCallback;
119+
118120
/**
119121
* Closure be used to be called after the field value stored.
120122
*/
@@ -205,6 +207,22 @@ public function fillCallback(callable|Closure $callback)
205207
return $this;
206208
}
207209

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+
208226
/**
209227
* Fill attribute with value from the request or delegate this action to the user defined callback.
210228
*
@@ -246,6 +264,12 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n
246264
$bulkRow
247265
);
248266

267+
$this->fillAttributeFromDefault(
268+
$request,
269+
$model,
270+
$this->label ?? $this->attribute
271+
);
272+
249273
$this->fillAttributeFromValue(
250274
$request,
251275
$model,
@@ -310,6 +334,23 @@ protected function fillAttributeFromValue(RestifyRequest $request, $model, $attr
310334
return $this;
311335
}
312336

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+
313354
/**
314355
* @return callable|string|null
315356
*/

0 commit comments

Comments
 (0)