Skip to content

Commit

Permalink
[4.x] Fix DataReferenceUpdater when field data from array is null (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Apr 24, 2024
1 parent 24039dc commit 99c6cbe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Data/DataReferenceUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,13 @@ protected function updateArrayValue($field, $dottedPrefix)

$dottedKey = $dottedPrefix.$field->handle();

$fieldData = collect(Arr::dot(Arr::get($data, $dottedKey, [])));
$fieldData = Arr::get($data, $dottedKey, []);

if (! $fieldData) {
return;
}

$fieldData = collect(Arr::dot($fieldData));

if (! $fieldData->contains($this->originalValue())) {
return;
Expand Down
28 changes: 28 additions & 0 deletions tests/Listeners/UpdateAssetReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,34 @@ public function it_updates_assets_fields_regardless_of_max_files_setting()
$this->assertEquals('surfboard.jpg', $entry->fresh()->get('products'));
}

/** @test */
public function it_updates_multi_assets_fields_even_when_existing_field_value_is_null()
{
$collection = tap(Facades\Collection::make('articles'))->save();

$this->setInBlueprints('collections/articles', [
'fields' => [
[
'handle' => 'pics',
'field' => [
'type' => 'assets',
'container' => 'test_container',
],
],
],
]);

$entry = tap(Facades\Entry::make()->collection($collection)->data([
'pics' => null,
]))->save();

$this->assertNull($entry->get('pics'));

$this->assetNorris->path('content/norris.jpg')->save();

$this->assertNull($entry->fresh()->get('pics'));
}

/** @test */
public function it_nullifies_references_when_deleting_an_asset()
{
Expand Down

0 comments on commit 99c6cbe

Please sign in to comment.