Skip to content
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

[5.x] Use default value from field config #9010

Merged
merged 9 commits into from
Apr 17, 2024
12 changes: 9 additions & 3 deletions src/Fields/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ public function raw()

public function value()
{
$raw = $this->raw;

if (! $this->fieldtype) {
return $this->raw;
return $raw;
}

if (! $raw && ($default = $this->fieldtype->field()?->defaultValue())) {
$raw = $default;
}

$value = $this->shallow
? $this->fieldtype->shallowAugment($this->raw)
: $this->fieldtype->augment($this->raw);
? $this->fieldtype->shallowAugment($raw)
: $this->fieldtype->augment($raw);

return $value;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Http/Resources/CP/Entries/ListedEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ protected function values($extra = [])
{
return $this->columns->mapWithKeys(function ($column) use ($extra) {
$key = $column->field;
$field = $this->blueprint->field($key);

if ($key === 'site') {
$value = $this->resource->locale();
} else {
$value = $extra[$key] ?? $this->resource->value($key);
$value = $extra[$key] ?? $this->resource->value($key) ?? $field?->defaultValue();
}

$field = $this->blueprint->field($key);

if (! $field) {
return [$key => $value];
}
Expand Down
11 changes: 8 additions & 3 deletions src/Http/Resources/CP/Taxonomies/ListedTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ protected function values($extra = [])
{
return $this->columns->mapWithKeys(function ($column) use ($extra) {
$key = $column->field;
$field = $this->blueprint->field($key);

if ($key == 'taxonomy') {
return [$key => $this->resource->taxonomy()->title()];
} else {
$value = $extra[$key] ?? $this->resource->value($key) ?? $field?->defaultValue();
}

$value = $this->blueprint
->field($key)
->setValue($extra[$key] ?? $this->resource->value($key))
if (! $field) {
return [$key => $value];
}

$value = $field->setValue($value)
->setParent($this->resource)
->preProcessIndex()
->value();
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Resources/CP/Users/ListedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ protected function values($extra = [])
{
return $this->columns->mapWithKeys(function ($column) use ($extra) {
$key = $column->field;
$field = $this->blueprint->field($key);

$value = $extra[$key] ?? $this->resource->value($key);
$value = $extra[$key] ?? $this->resource->value($key) ?? $field?->defaultValue();

if ($field = $this->blueprint->field($key)) {
if ($field) {
$value = $field->setValue($value)->preProcessIndex()->value();
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Fields/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Fields;

use Statamic\Facades\Blueprint;
use Statamic\Fields\Field;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\Value;
use Tests\TestCase;
Expand Down Expand Up @@ -153,6 +154,30 @@ public function augment($data)

$this->assertEquals('{"foo":"BAR!","baz":{"id":"123","title":"Title for 123"},"qux":[{"id":"456","title":"Title for 456"},{"id":"789","title":"Title for 789"}]}', json_encode($value));
}

/** @test */
public function it_uses_the_default_value()
{
$fieldtype = new class extends Fieldtype
{
public function augment($value)
{
return $value.'!';
}
};

$fieldtype->setField(new Field('search_engine_url', ['default' => 'https://google.com']));

tap(new Value(null, null, $fieldtype), function ($value) {
$this->assertNull($value->raw());
$this->assertEquals('https://google.com!', $value->value());
});

tap(new Value('foo', null, $fieldtype), function ($value) {
$this->assertEquals('foo', $value->raw());
$this->assertEquals('foo!', $value->value());
});
}
}

class DummyAugmentable implements \Statamic\Contracts\Data\Augmentable
Expand Down