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 @@ -39,13 +39,19 @@ public function raw()

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

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

if (! $rawValue && $this->fieldtype->config('default')) {
$rawValue = $this->fieldtype->config('default');
jasonvarga marked this conversation as resolved.
Show resolved Hide resolved
}

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

return $value;
}
Expand Down
15 changes: 15 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,20 @@ 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_gets_the_default_if_the_value_is_empty()
{
$fieldtype = new class extends Fieldtype
{
};

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

$value = new Value(null, null, $fieldtype);

$this->assertEquals('https://google.com', $value->value());
}
}

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