Skip to content

Commit acb605e

Browse files
Fix undefined array key error from FieldUpdater (#107)
* Add failing test * Refactor how we handle imported fields in `FieldUpdater` * Fix styling --------- Co-authored-by: duncanmcclean <duncanmcclean@users.noreply.github.com>
1 parent f4cd2f8 commit acb605e

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

src/Support/FieldUpdater.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public function blueprint(Blueprint $blueprint): self
2929

3030
public function updateFieldConfig(array $config): void
3131
{
32-
if ($prefix = $this->field->prefix()) {
33-
$this->updatePrefixedField($prefix, $config);
32+
if ($linkedField = $this->getLinkedField()) {
33+
$this->updateLinkedField($linkedField, $config);
3434

3535
return;
3636
}
3737

38-
if ($importedField = $this->getImportedField()) {
39-
$this->updateImportedField($importedField, $config);
38+
if ($this->isImportedField()) {
39+
$this->updateImportedField($config, $this->field->prefix());
4040

4141
return;
4242
}
@@ -49,7 +49,7 @@ public function updateFieldConfig(array $config): void
4949
$this->blueprint->save();
5050
}
5151

52-
private function getImportedField(): ?array
52+
private function getLinkedField(): ?array
5353
{
5454
return $this->blueprint->fields()->items()
5555
->where('handle', $this->field->handle())
@@ -58,13 +58,13 @@ private function getImportedField(): ?array
5858
}
5959

6060
/**
61-
* This method handles updating imported fields from fieldsets.
61+
* This method handles updating linked fields from fieldsets.
6262
*
6363
* -
6464
* handle: foo
6565
* field: fieldset.foo
6666
*/
67-
private function updateImportedField(array $importedField, array $config): void
67+
private function updateLinkedField(array $importedField, array $config): void
6868
{
6969
/** @var \Statamic\Fields\Fieldset $fieldset */
7070
$fieldHandle = Str::after($importedField['field'], '.');
@@ -92,21 +92,34 @@ private function updateImportedField(array $importedField, array $config): void
9292
}
9393

9494
/**
95-
* This method handles updating imported fields from fieldsets, which use a prefix.
95+
* Determines if a field is imported from a fieldset by checking if it exists in the blueprint's top-level fields.
96+
*/
97+
private function isImportedField(): bool
98+
{
99+
$topLevelFieldHandles = $this->blueprint->tabs()
100+
->flatMap(fn ($tab) => $tab->sections()->flatMap(fn ($section) => $section->fields()->items()))
101+
->pluck('handle')
102+
->filter();
103+
104+
return $this->blueprint->hasField($this->field->handle()) && ! $topLevelFieldHandles->contains($this->field->handle());
105+
}
106+
107+
/**
108+
* This method handles updating imported fields from fieldsets, either with or without prefixes.
96109
*
97110
* -
98111
* import: fieldset
99112
* prefix: foo_
100113
*/
101-
private function updatePrefixedField(string $prefix, array $config): void
114+
private function updateImportedField(array $config, ?string $prefix = null): void
102115
{
103116
/** @var \Statamic\Fields\Fieldset $fieldset */
104117
$fieldset = $this->blueprint->fields()->items()
105118
->filter(fn (array $field) => isset($field['import']))
106119
->map(fn (array $field) => Fieldset::find($field['import']))
107120
->filter(function ($fieldset) use ($prefix) {
108121
return collect($fieldset->fields()->items())
109-
->where('handle', Str::after($this->field->handle(), $prefix))
122+
->where('handle', Str::after($this->field->handle(), $prefix ?? ''))
110123
->isNotEmpty();
111124
})
112125
->first();
@@ -115,7 +128,7 @@ private function updatePrefixedField(string $prefix, array $config): void
115128
...$fieldset->contents(),
116129
'fields' => collect($fieldset->contents()['fields'])
117130
->map(function (array $field) use ($config, $prefix) {
118-
if ($field['handle'] === Str::after($this->field->handle(), $prefix)) {
131+
if ($field['handle'] === Str::after($this->field->handle(), $prefix ?? '')) {
119132
return [
120133
'handle' => $field['handle'],
121134
'field' => $config,

tests/Transformers/BardTransformerTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public function it_enables_buttons_on_bard_field()
384384
}
385385

386386
#[Test]
387-
public function it_enables_buttons_on_imported_bard_field()
387+
public function it_enables_buttons_on_linked_bard_field()
388388
{
389389
Fieldset::make('content_stuff')->setContents(['fields' => [
390390
['handle' => 'bard_field', 'field' => ['type' => 'bard']],
@@ -421,6 +421,44 @@ public function it_enables_buttons_on_imported_bard_field()
421421
], $fieldset->field('bard_field')->get('buttons'));
422422
}
423423

424+
#[Test]
425+
public function it_enables_buttons_on_imported_bard_field()
426+
{
427+
Fieldset::make('content_stuff')->setContents(['fields' => [
428+
['handle' => 'bard_field', 'field' => ['type' => 'bard']],
429+
]])->save();
430+
431+
$blueprint = $this->collection->entryBlueprint();
432+
433+
$this->blueprint->setContents([
434+
'sections' => [
435+
'main' => [
436+
'fields' => [
437+
['import' => 'content_stuff'],
438+
],
439+
],
440+
],
441+
])->save();
442+
443+
$transformer = new BardTransformer(
444+
import: $this->import,
445+
blueprint: $blueprint,
446+
field: $blueprint->field('bard_field'),
447+
config: []
448+
);
449+
450+
$transformer->transform('<h2 style="text-align: center;"><strong>Hello</strong> <em>world</em>!</h2>');
451+
452+
$fieldset = Fieldset::find('content_stuff');
453+
454+
$this->assertEquals([
455+
'h2',
456+
'aligncenter',
457+
'bold',
458+
'italic',
459+
], $fieldset->field('bard_field')->get('buttons'));
460+
}
461+
424462
#[Test]
425463
public function it_enables_buttons_on_imported_bard_field_with_prefix()
426464
{

0 commit comments

Comments
 (0)