Skip to content

9.6.0 #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 4, 2024
Merged

9.6.0 #619

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public function getStoringRules(): array

// Modify validation to accept URLs
foreach ($rules as &$rule) {
if (is_string($rule) && Str::startsWith($rule, 'file')) {
if (is_string($rule) && str($rule)->startsWith('file')) {
$rule = 'sometimes|'.$rule;
}
}
Expand Down