Skip to content

Commit

Permalink
9.6.0 (#619)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
binaryk and binaryk authored Dec 4, 2024
1 parent a2f8aaa commit b15e3f6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs-v2/content/en/api/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,18 @@ Now, for the fields that don't have a description into the database, it will ret
The default value is ONLY used for the READ, not for WRITE requests.
</alert>

### Default Stored Value

During any (update or store requests), this is called after the fill and store callbacks.

You can pass a callable or a value, and it will be attached to the model if no value provided otherwise.

Imagine it's like `attributes` in the model:

```php
field('currency')->defaultCallback('EUR'),
```

## Customizations

### Field label
Expand Down
41 changes: 41 additions & 0 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class Field extends OrganicField implements JsonSerializable
*/
protected $valueCallback;

protected $fillDefaultCallback;

/**
* Closure be used to be called after the field value stored.
*/
Expand Down Expand Up @@ -205,6 +207,22 @@ public function fillCallback(callable|Closure $callback)
return $this;
}

/**
* This is called after the fill and store callbacks.
*
* You can pass a callable or a value, and it will be attached to the model if no value provided otherwise.
*
* Imagine it's like `attributes` in the model.
*
* @return $this
*/
public function defaultCallback(mixed $callback)
{
$this->defaultCallback = $callback;

return $this;
}

/**
* Fill attribute with value from the request or delegate this action to the user defined callback.
*
Expand Down Expand Up @@ -246,6 +264,12 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n
$bulkRow
);

$this->fillAttributeFromDefault(
$request,
$model,
$this->label ?? $this->attribute
);

$this->fillAttributeFromValue(
$request,
$model,
Expand Down Expand Up @@ -310,6 +334,23 @@ protected function fillAttributeFromValue(RestifyRequest $request, $model, $attr
return $this;
}

protected function fillAttributeFromDefault(RestifyRequest $request, $model, $attribute)
{
if ($model->{$attribute}) {
return $this;
}

if (! isset($this->fillDefaultCallback)) {
return $this;
}

$model->{$attribute} = is_callable($this->fillDefaultCallback)
? call_user_func($this->fillDefaultCallback, $request, $model, $attribute)
: $this->fillDefaultCallback;

return $this;
}

/**
* @return callable|string|null
*/
Expand Down

0 comments on commit b15e3f6

Please sign in to comment.