Skip to content

Commit

Permalink
[5.x] Support the Group fieldtype in DataReferenceUpdater (#11410)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Feb 5, 2025
1 parent 053a48d commit f0a74b4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Data/DataReferenceUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected function updateNestedFieldValues($fields, $dottedPrefix)
{
$fields
->filter(function ($field) {
return in_array($field->type(), ['replicator', 'grid', 'bard']);
return in_array($field->type(), ['replicator', 'grid', 'group', 'bard']);
})
->each(function ($field) use ($dottedPrefix) {
$method = 'update'.ucfirst($field->type()).'Children';
Expand Down Expand Up @@ -155,6 +155,24 @@ protected function updateGridChildren($field, $dottedKey)
});
}

/**
* Update group field children.
*
* @param \Statamic\Fields\Field $field
* @param string $dottedKey
*/
protected function updateGroupChildren($field, $dottedKey)
{
$data = $this->item->data();

$dottedPrefix = "{$dottedKey}.";
$fields = Arr::get($field->config(), 'fields');

if ($fields) {
$this->recursivelyUpdateFields((new Fields($fields))->all(), $dottedPrefix);
}
}

/**
* Update bard field children.
*
Expand Down
50 changes: 50 additions & 0 deletions tests/Listeners/UpdateAssetReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,56 @@ public function it_updates_nested_asset_fields_within_grid_fields()
$this->assertEquals(['content/norris.jpg', 'lee.jpg'], Arr::get($entry->fresh()->data(), 'griddy.1.pics'));
}

#[Test]
public function it_updates_nested_asset_fields_within_group_fields()
{
$collection = tap(Facades\Collection::make('articles'))->save();

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

$entry = tap(Facades\Entry::make()->collection($collection)->data([
'group_field' => [
'product' => 'hoff.jpg',
'pics' => ['hoff.jpg', 'norris.jpg', 'lee.jpg'],
],
]))->save();

$this->assertEquals('hoff.jpg', Arr::get($entry->data(), 'group_field.product'));
$this->assertEquals(['hoff.jpg', 'norris.jpg', 'lee.jpg'], Arr::get($entry->data(), 'group_field.pics'));

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

$this->assertFalse(Arr::has($entry->fresh()->data(), 'group_field.product'));
$this->assertEquals(['content/norris.jpg', 'lee.jpg'], Arr::get($entry->fresh()->data(), 'group_field.pics'));
}

#[Test]
public function it_updates_nested_asset_fields_within_bard_fields()
{
Expand Down

0 comments on commit f0a74b4

Please sign in to comment.