Skip to content

Commit d714d7e

Browse files
committed
Refactor how we handle imported fields in FieldUpdater
1 parent 5e9c501 commit d714d7e

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/Support/FieldUpdater.php

Lines changed: 29 additions & 14 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,22 +49,22 @@ 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())
56-
->filter(fn (array $field) => isset($field['field']) && is_string($field['field']))
56+
->filter(fn(array $field) => isset($field['field']) && is_string($field['field']))
5757
->first();
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,36 @@ 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+
* @return bool
98+
*/
99+
private function isImportedField(): bool
100+
{
101+
$topLevelFieldHandles = $this->blueprint->tabs()
102+
->flatMap(fn($tab) => $tab->sections()->flatMap(fn($section) => $section->fields()->items()))
103+
->pluck('handle')
104+
->filter();
105+
106+
return $this->blueprint->hasField($this->field->handle()) && ! $topLevelFieldHandles->contains($this->field->handle());
107+
}
108+
109+
/**
110+
* This method handles updating imported fields from fieldsets, either with or without prefixes.
96111
*
97112
* -
98113
* import: fieldset
99114
* prefix: foo_
100115
*/
101-
private function updatePrefixedField(string $prefix, array $config): void
116+
private function updateImportedField(array $config, ?string $prefix = null): void
102117
{
103118
/** @var \Statamic\Fields\Fieldset $fieldset */
104119
$fieldset = $this->blueprint->fields()->items()
105-
->filter(fn (array $field) => isset($field['import']))
106-
->map(fn (array $field) => Fieldset::find($field['import']))
120+
->filter(fn(array $field) => isset($field['import']))
121+
->map(fn(array $field) => Fieldset::find($field['import']))
107122
->filter(function ($fieldset) use ($prefix) {
108123
return collect($fieldset->fields()->items())
109-
->where('handle', Str::after($this->field->handle(), $prefix))
124+
->where('handle', Str::after($this->field->handle(), $prefix ?? ''))
110125
->isNotEmpty();
111126
})
112127
->first();
@@ -115,7 +130,7 @@ private function updatePrefixedField(string $prefix, array $config): void
115130
...$fieldset->contents(),
116131
'fields' => collect($fieldset->contents()['fields'])
117132
->map(function (array $field) use ($config, $prefix) {
118-
if ($field['handle'] === Str::after($this->field->handle(), $prefix)) {
133+
if ($field['handle'] === Str::after($this->field->handle(), $prefix ?? '')) {
119134
return [
120135
'handle' => $field['handle'],
121136
'field' => $config,

tests/Transformers/BardTransformerTest.php

Lines changed: 2 additions & 2 deletions
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']],
@@ -422,7 +422,7 @@ public function it_enables_buttons_on_imported_bard_field()
422422
}
423423

424424
#[Test]
425-
public function it_enables_buttons_on_imported_bard_field_without_prefix()
425+
public function it_enables_buttons_on_imported_bard_field()
426426
{
427427
Fieldset::make('content_stuff')->setContents(['fields' => [
428428
['handle' => 'bard_field', 'field' => ['type' => 'bard']],

0 commit comments

Comments
 (0)