3
3
namespace Statamic \Importer \Support ;
4
4
5
5
use Statamic \Facades \Blink ;
6
- use Statamic \Facades \Fieldset ;
7
6
use Statamic \Fields \Blueprint ;
8
7
use Statamic \Fields \Field ;
8
+ use Statamic \Fields \Fieldset ;
9
9
use Statamic \Support \Str ;
10
10
11
11
class FieldUpdater
@@ -27,6 +27,13 @@ public function blueprint(Blueprint $blueprint): self
27
27
return $ this ;
28
28
}
29
29
30
+ public function fieldset (Fieldset $ fieldset ): self
31
+ {
32
+ $ this ->blueprint = $ fieldset ;
33
+
34
+ return $ this ;
35
+ }
36
+
30
37
public function updateFieldConfig (array $ config ): void
31
38
{
32
39
if ($ linkedField = $ this ->getLinkedField ()) {
@@ -96,12 +103,9 @@ private function updateLinkedField(array $importedField, array $config): void
96
103
*/
97
104
private function isImportedField (): bool
98
105
{
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 ();
103
107
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 ());
105
109
}
106
110
107
111
/**
@@ -116,14 +120,36 @@ private function updateImportedField(array $config, ?string $prefix = null): voi
116
120
/** @var \Statamic\Fields\Fieldset $fieldset */
117
121
$ fieldset = $ this ->blueprint ->fields ()->items ()
118
122
->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
+
121
143
return collect ($ fieldset ->fields ()->items ())
122
- ->where ('handle ' , Str::after ($ this ->field ->handle (), $ prefix ?? '' ))
144
+ ->where ('handle ' , Str::after ($ this ->field ->handle (), $ prefix ))
123
145
->isNotEmpty ();
124
146
})
125
147
->first ();
126
148
149
+ if (! $ fieldset ) {
150
+ return ;
151
+ }
152
+
127
153
$ fieldset ->setContents ([
128
154
...$ fieldset ->contents (),
129
155
'fields ' => collect ($ fieldset ->contents ()['fields ' ])
0 commit comments