Skip to content

Commit 38072ba

Browse files
committed
Support the Group fieldtype in DataReferenceUpdater
1 parent 6969fb6 commit 38072ba

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/Data/DataReferenceUpdater.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected function updateNestedFieldValues($fields, $dottedPrefix)
9898
{
9999
$fields
100100
->filter(function ($field) {
101-
return in_array($field->type(), ['replicator', 'grid', 'bard']);
101+
return in_array($field->type(), ['replicator', 'grid', 'group', 'bard']);
102102
})
103103
->each(function ($field) use ($dottedPrefix) {
104104
$method = 'update'.ucfirst($field->type()).'Children';
@@ -155,6 +155,24 @@ protected function updateGridChildren($field, $dottedKey)
155155
});
156156
}
157157

158+
/**
159+
* Update group field children.
160+
*
161+
* @param \Statamic\Fields\Field $field
162+
* @param string $dottedKey
163+
*/
164+
protected function updateGroupChildren($field, $dottedKey)
165+
{
166+
$data = $this->item->data();
167+
168+
$dottedPrefix = "{$dottedKey}.";
169+
$fields = Arr::get($field->config(), 'fields');
170+
171+
if ($fields) {
172+
$this->recursivelyUpdateFields((new Fields($fields))->all(), $dottedPrefix);
173+
}
174+
}
175+
158176
/**
159177
* Update bard field children.
160178
*

tests/Listeners/UpdateAssetReferencesTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,56 @@ public function it_updates_nested_asset_fields_within_grid_fields()
707707
$this->assertEquals(['content/norris.jpg', 'lee.jpg'], Arr::get($entry->fresh()->data(), 'griddy.1.pics'));
708708
}
709709

710+
#[Test]
711+
public function it_updates_nested_asset_fields_within_group_fields()
712+
{
713+
$collection = tap(Facades\Collection::make('articles'))->save();
714+
715+
$this->setInBlueprints('collections/articles', [
716+
'fields' => [
717+
[
718+
'handle' => 'group_field',
719+
'field' => [
720+
'type' => 'group',
721+
'fields' => [
722+
[
723+
'handle' => 'product',
724+
'field' => [
725+
'type' => 'assets',
726+
'container' => 'test_container',
727+
'max_files' => 1,
728+
],
729+
],
730+
[
731+
'handle' => 'pics',
732+
'field' => [
733+
'type' => 'assets',
734+
'container' => 'test_container',
735+
],
736+
],
737+
],
738+
],
739+
],
740+
],
741+
]);
742+
743+
$entry = tap(Facades\Entry::make()->collection($collection)->data([
744+
'group_field' => [
745+
'product' => 'hoff.jpg',
746+
'pics' => ['hoff.jpg', 'norris.jpg', 'lee.jpg'],
747+
],
748+
]))->save();
749+
750+
$this->assertEquals('hoff.jpg', Arr::get($entry->data(), 'group_field.product'));
751+
$this->assertEquals(['hoff.jpg', 'norris.jpg', 'lee.jpg'], Arr::get($entry->data(), 'group_field.pics'));
752+
753+
$this->assetNorris->path('content/norris.jpg')->save();
754+
$this->assetHoff->delete();
755+
756+
$this->assertFalse(Arr::has($entry->fresh()->data(), 'group_field.product'));
757+
$this->assertEquals(['content/norris.jpg', 'lee.jpg'], Arr::get($entry->fresh()->data(), 'group_field.pics'));
758+
}
759+
710760
#[Test]
711761
public function it_updates_nested_asset_fields_within_bard_fields()
712762
{

0 commit comments

Comments
 (0)