Skip to content

Commit dba698e

Browse files
Handle nested fieldsets in FieldUpdater (#108)
* wip * Fix styling * don't need the facade * wip * Fix styling --------- Co-authored-by: duncanmcclean <[email protected]>
1 parent acb605e commit dba698e

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

src/Support/FieldUpdater.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Statamic\Importer\Support;
44

55
use Statamic\Facades\Blink;
6-
use Statamic\Facades\Fieldset;
76
use Statamic\Fields\Blueprint;
87
use Statamic\Fields\Field;
8+
use Statamic\Fields\Fieldset;
99
use Statamic\Support\Str;
1010

1111
class FieldUpdater
@@ -27,6 +27,13 @@ public function blueprint(Blueprint $blueprint): self
2727
return $this;
2828
}
2929

30+
public function fieldset(Fieldset $fieldset): self
31+
{
32+
$this->blueprint = $fieldset;
33+
34+
return $this;
35+
}
36+
3037
public function updateFieldConfig(array $config): void
3138
{
3239
if ($linkedField = $this->getLinkedField()) {
@@ -96,12 +103,9 @@ private function updateLinkedField(array $importedField, array $config): void
96103
*/
97104
private function isImportedField(): bool
98105
{
99-
$topLevelFieldHandles = $this->blueprint->tabs()
100-
->flatMap(fn ($tab) => $tab->sections()->flatMap(fn ($section) => $section->fields()->items()))
101-
->pluck('handle')
102-
->filter();
106+
$topLevelFieldHandles = $this->blueprint->fields()->items()->pluck('handle')->filter();
103107

104-
return $this->blueprint->hasField($this->field->handle()) && ! $topLevelFieldHandles->contains($this->field->handle());
108+
return $this->blueprint->field($this->field->handle()) && ! $topLevelFieldHandles->contains($this->field->handle());
105109
}
106110

107111
/**
@@ -116,14 +120,36 @@ private function updateImportedField(array $config, ?string $prefix = null): voi
116120
/** @var \Statamic\Fields\Fieldset $fieldset */
117121
$fieldset = $this->blueprint->fields()->items()
118122
->filter(fn (array $field) => isset($field['import']))
119-
->map(fn (array $field) => Fieldset::find($field['import']))
120-
->filter(function ($fieldset) use ($prefix) {
123+
->mapWithKeys(fn (array $field) => [
124+
$field['prefix'] ?? '' => Fieldset::find($field['import']),
125+
])
126+
->filter(function (Fieldset $fieldset, string $prefix) use ($config) {
127+
// When the field exists in the fieldset, but it's not a top-level field,
128+
// pass the Fieldset to the FieldUpdater (this class) to update the field config.
129+
$fieldHandleWithoutBlueprintPrefix = Str::after($this->field->handle(), $prefix);
130+
131+
if (
132+
($field = $fieldset->field($fieldHandleWithoutBlueprintPrefix))
133+
&& ! $fieldset->fields()->items()->pluck('handle')->filter()->contains($fieldHandleWithoutBlueprintPrefix)
134+
) {
135+
(new self)
136+
->field($field)
137+
->fieldset($fieldset)
138+
->updateFieldConfig($config);
139+
140+
return false;
141+
}
142+
121143
return collect($fieldset->fields()->items())
122-
->where('handle', Str::after($this->field->handle(), $prefix ?? ''))
144+
->where('handle', Str::after($this->field->handle(), $prefix))
123145
->isNotEmpty();
124146
})
125147
->first();
126148

149+
if (! $fieldset) {
150+
return;
151+
}
152+
127153
$fieldset->setContents([
128154
...$fieldset->contents(),
129155
'fields' => collect($fieldset->contents()['fields'])

tests/Transformers/BardTransformerTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,4 +496,46 @@ public function it_enables_buttons_on_imported_bard_field_with_prefix()
496496
'italic',
497497
], $fieldset->field('bard_field')->get('buttons'));
498498
}
499+
500+
#[Test]
501+
public function it_enables_buttons_on_imported_bard_field_in_nested_fieldsets()
502+
{
503+
Fieldset::make('actual_content_stuff')->setContents(['fields' => [
504+
['handle' => 'bard_field', 'field' => ['type' => 'bard']],
505+
]])->save();
506+
507+
Fieldset::make('content_stuff')->setContents(['fields' => [
508+
['import' => 'actual_content_stuff', 'prefix' => 'actual_'],
509+
]])->save();
510+
511+
$blueprint = $this->collection->entryBlueprint();
512+
513+
$this->blueprint->setContents([
514+
'sections' => [
515+
'main' => [
516+
'fields' => [
517+
['import' => 'content_stuff', 'prefix' => 'resources_'],
518+
],
519+
],
520+
],
521+
])->save();
522+
523+
$transformer = new BardTransformer(
524+
import: $this->import,
525+
blueprint: $blueprint,
526+
field: $blueprint->field('resources_actual_bard_field'),
527+
config: []
528+
);
529+
530+
$transformer->transform('<h2 style="text-align: center;"><strong>Hello</strong> <em>world</em>!</h2>');
531+
532+
$fieldset = Fieldset::find('actual_content_stuff');
533+
534+
$this->assertEquals([
535+
'h2',
536+
'aligncenter',
537+
'bold',
538+
'italic',
539+
], $fieldset->field('bard_field')->get('buttons'));
540+
}
499541
}

0 commit comments

Comments
 (0)