Skip to content

Commit

Permalink
[5.x] Prevent falsey values from returning blueprint defaults (#9990)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster authored May 3, 2024
1 parent 341f506 commit d474d67
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Fields/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function value()
return $raw;
}

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

$value = $this->shallow
Expand Down
36 changes: 36 additions & 0 deletions tests/Fields/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,42 @@ public function augment($value)
$this->assertEquals('foo!', $value->value());
});
}

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

$fieldtype->setField(new Field('the_handle', ['default' => true]));

tap(new Value(false, null, $fieldtype), function ($value) {
$this->assertSame(false, $value->value());
});
}

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

$fieldtype->setField(new Field('the_handle', ['default' => false]));

tap(new Value(null, null, $fieldtype), function ($value) {
$this->assertSame(false, $value->value());
});
}
}

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

0 comments on commit d474d67

Please sign in to comment.