Skip to content
Merged
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
24 changes: 17 additions & 7 deletions src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function makeModelFromContract(EntryContract $source)

$date = $source->hasDate() ? $source->date() : null;

$origin = $source->origin();
$directOrigin = $source->origin();

if ($template = $source->get('template', $source->template)) {
$data->put('template', $template);
Expand All @@ -82,26 +82,36 @@ public static function makeModelFromContract(EntryContract $source)
$data->forget('template');
}

$originData = $origin->data();
$directOriginData = $directOrigin->data();

// remove any fields in entry data that are marked as localized but value is present, and matches origin
// remove any fields in entry data that are marked as localized but value is present, and does not match origin
Copy link
Copy Markdown
Contributor

@Boefjim Boefjim Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryanmitchell

I think this reverted a fix for this comment on the 5.x branch, that had not been applied on the 4.x branch?

This should only remove fields that are present & match with the origin (or now rather the closest origin that localizes the field), so the original comment here seems to be more accurate 🤔

Copy link
Copy Markdown
Contributor Author

@ryanmitchell ryanmitchell Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've updated that directly on 5.x

$localizedFields = [];
foreach ($localizedBlueprintFields as $blueprintField) {
if ($data->has($blueprintField)) {
if ($data->get($blueprintField) === $originData->get($blueprintField)) {
$fieldOrigin = $directOrigin;
$fieldOriginData = $directOriginData;
while (
$fieldOrigin->hasOrigin()
&& ! in_array($blueprintField, $fieldOriginData->get('__localized_fields') ?? [])
) {
$fieldOrigin = $fieldOrigin->origin();
$fieldOriginData = $fieldOrigin->data();
}

if ($data->get($blueprintField) === $fieldOriginData->get($blueprintField)) {
$data->forget($blueprintField);
} else {
$localizedFields[] = $blueprintField;
}
}
}

$data = $originData->merge($data);
$data = $directOrigin->data()->merge($data);

$data->put('__localized_fields', $localizedFields);

if (! in_array('date', $localizedFields)) {
$date = $origin->hasDate() ? $origin->date() : null;
$date = $directOrigin->hasDate() ? $directOrigin->date() : null;
}
}
}
Expand All @@ -126,7 +136,7 @@ public static function makeModelFromContract(EntryContract $source)

$attributes = [
...$attributes,
'origin_id' => $origin?->id(),
'origin_id' => $directOrigin?->id(),
'site' => $source->locale(),
'slug' => $source->slug(),
'uri' => $source->uri() ?? $source->routableUri(),
Expand Down
48 changes: 48 additions & 0 deletions tests/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,52 @@ public function it_build_stache_associations_when_taxonomy_driver_is_not_eloquen

$this->assertCount(2, $taxonomyStore->store('test')->index('associations')->items());
}

#[Test]
public function it_localizes_null_values_for_non_direct_descendants()
{
$this->setSites([
'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'],
'nl' => ['name' => 'Dutch', 'locale' => 'nl_NL', 'url' => 'http://nl.test.com/'],
'nl-BE' => ['name' => 'Flemish', 'locale' => 'nl_BE', 'url' => 'http://test.com/nl-be/'],
]);

$blueprint = Facades\Blueprint::makeFromFields(['foo' => ['type' => 'text', 'localizable' => true]])->setHandle('test');
$blueprint->save();

BlueprintRepository::shouldReceive('in')->with('collections/pages')->andReturn(collect(['test' => $blueprint]));

$collection = (new Collection)
->handle('pages')
->propagate(true)
->sites(['en', 'nl', 'nl-BE'])
->save();

/** @var Entry $originEntry */
$originEntry = (new Entry)
->id(1)
->locale('en')
->collection($collection)
->blueprint('test')
->data(['foo' => 'bar']);
$originEntry->save();

/** @var Entry $directLocalizationEntry */
$directLocalizationEntry = $originEntry->in('nl');
$directLocalizationEntry->toModel()->save();
$indirectLocalizationEntry = $directLocalizationEntry->in('nl-BE');
$indirectLocalizationEntry->origin($directLocalizationEntry);

$this->assertSame($originEntry, $directLocalizationEntry->origin());
$this->assertSame($directLocalizationEntry, $indirectLocalizationEntry->origin());

$indirectLocalizationEntry->data(['foo' => null]);
$indirectLocalizationEntry->toModel()->save();

$directLocalizationEntry = Entry::fromModel($directLocalizationEntry->model()->fresh());
$indirectLocalizationEntry = Entry::fromModel($indirectLocalizationEntry->model()->fresh());

$this->assertEquals('bar', $directLocalizationEntry->value('foo'));
$this->assertEquals(null, $indirectLocalizationEntry->value('foo'));
}
}