Skip to content

Commit 9b9f3fb

Browse files
committed
wip
1 parent acb605e commit 9b9f3fb

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

src/Support/FieldUpdater.php

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

55
use Statamic\Facades\Blink;
6-
use Statamic\Facades\Fieldset;
6+
use Statamic\Fields\Fieldset;
77
use Statamic\Fields\Blueprint;
88
use Statamic\Fields\Field;
99
use Statamic\Support\Str;
10+
use Facades\Statamic\Importer\Support\FieldUpdater as FieldUpdaterFacade;
1011

1112
class FieldUpdater
1213
{
@@ -20,7 +21,7 @@ public function field(Field $field): self
2021
return $this;
2122
}
2223

23-
public function blueprint(Blueprint $blueprint): self
24+
public function blueprint(Blueprint|Fieldset $blueprint): self
2425
{
2526
$this->blueprint = $blueprint;
2627

@@ -53,7 +54,7 @@ private function getLinkedField(): ?array
5354
{
5455
return $this->blueprint->fields()->items()
5556
->where('handle', $this->field->handle())
56-
->filter(fn (array $field) => isset($field['field']) && is_string($field['field']))
57+
->filter(fn(array $field) => isset($field['field']) && is_string($field['field']))
5758
->first();
5859
}
5960

@@ -93,15 +94,14 @@ private function updateLinkedField(array $importedField, array $config): void
9394

9495
/**
9596
* Determines if a field is imported from a fieldset by checking if it exists in the blueprint's top-level fields.
97+
*
98+
* @return bool
9699
*/
97100
private function isImportedField(): bool
98101
{
99-
$topLevelFieldHandles = $this->blueprint->tabs()
100-
->flatMap(fn ($tab) => $tab->sections()->flatMap(fn ($section) => $section->fields()->items()))
101-
->pluck('handle')
102-
->filter();
102+
$topLevelFieldHandles = $this->blueprint->fields()->items()->pluck('handle')->filter();
103103

104-
return $this->blueprint->hasField($this->field->handle()) && ! $topLevelFieldHandles->contains($this->field->handle());
104+
return $this->blueprint->field($this->field->handle()) && ! $topLevelFieldHandles->contains($this->field->handle());
105105
}
106106

107107
/**
@@ -116,14 +116,33 @@ private function updateImportedField(array $config, ?string $prefix = null): voi
116116
/** @var \Statamic\Fields\Fieldset $fieldset */
117117
$fieldset = $this->blueprint->fields()->items()
118118
->filter(fn (array $field) => isset($field['import']))
119-
->map(fn (array $field) => Fieldset::find($field['import']))
120-
->filter(function ($fieldset) use ($prefix) {
119+
->mapWithKeys(fn (array $field) => [$field['prefix'] ?? '' => Fieldset::find($field['import'])])
120+
->filter(function ($fieldset, $prefix) use ($config) {
121+
// When the field exists in the fieldset, but it's not a top-level field,
122+
// pass the Fieldset to the FieldUpdater (this class) to update the field config.
123+
$fieldHandleWithoutBlueprintPrefix = Str::after($this->field->handle(), $prefix);
124+
125+
if (
126+
($field = $fieldset->field($fieldHandleWithoutBlueprintPrefix))
127+
&& ! $fieldset->fields()->items()->pluck('handle')->filter()->contains($fieldHandleWithoutBlueprintPrefix)
128+
) {
129+
FieldUpdaterFacade::field($field)
130+
->blueprint($fieldset)
131+
->updateFieldConfig($config);
132+
133+
return false;
134+
}
135+
121136
return collect($fieldset->fields()->items())
122-
->where('handle', Str::after($this->field->handle(), $prefix ?? ''))
137+
->where('handle', Str::after($this->field->handle(), $prefix))
123138
->isNotEmpty();
124139
})
125140
->first();
126141

142+
if (! $fieldset) {
143+
return;
144+
}
145+
127146
$fieldset->setContents([
128147
...$fieldset->contents(),
129148
'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)