Skip to content

Commit d98c231

Browse files
authored
[4.x] Fix empty localized values not being set properly (#474)
* Fix empty localized values not being set properly * Fix code style * Add test for nulled fields
1 parent 7cf809a commit d98c231

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Entries/Entry.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class Entry extends FileEntry
1515

1616
public static function fromModel(Model $model)
1717
{
18-
$data = isset($model->data['__localized_fields']) ? collect($model->data)->only($model->data['__localized_fields']) : $model->data;
18+
$data = isset($model->data['__localized_fields'])
19+
? collect($model->data['__localized_fields'])->mapWithKeys(fn ($field) => [$field => $model->data[$field] ?? null])
20+
: $model->data;
1921

2022
foreach ((new self)->getDataColumnMappings($model) as $key) {
2123
$data[$key] = $model->$key;

tests/Entries/EntryTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,43 @@ public function it_propagates_origin_date_to_descendent_models()
250250
$this->assertEquals($entry->descendants()->get('fr')->model()->date, '2024-01-01 00:00:00');
251251
}
252252

253+
#[Test]
254+
public function it_localizes_null_fields()
255+
{
256+
$this->setSites([
257+
'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'],
258+
'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'],
259+
'es' => ['name' => 'Spanish', 'locale' => 'es_ES', 'url' => 'http://test.com/es/'],
260+
'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://test.com/de/'],
261+
]);
262+
263+
$blueprint = Facades\Blueprint::makeFromFields(['foo' => ['type' => 'text', 'localizable' => true]])->setHandle('test');
264+
$blueprint->save();
265+
266+
BlueprintRepository::shouldReceive('in')->with('collections/pages')->andReturn(collect(['test' => $blueprint]));
267+
268+
$collection = (new Collection)
269+
->handle('pages')
270+
->propagate(true)
271+
->sites(['en', 'fr', 'es', 'de'])
272+
->save();
273+
274+
$entry = (new Entry)
275+
->id(1)
276+
->locale('en')
277+
->collection($collection)
278+
->blueprint('test')
279+
->data(['foo' => 'bar']);
280+
281+
$entry->save();
282+
$entry->descendants()->get('fr')->data(['foo' => null])->save();
283+
$entry->descendants()->get('es')->data(['foo' => 'baz'])->save();
284+
285+
$this->assertNull($entry->descendants()->get('fr')->foo ?? null);
286+
$this->assertEquals('bar', $entry->descendants()->get('de')->foo ?? null);
287+
$this->assertEquals('baz', $entry->descendants()->get('es')->foo ?? null);
288+
}
289+
253290
#[Test]
254291
public function it_stores_and_retrieves_mapped_data_values()
255292
{

0 commit comments

Comments
 (0)