33namespace Statamic \Importer \Support ;
44
55use Statamic \Facades \Blink ;
6- use Statamic \Facades \Fieldset ;
6+ use Statamic \Fields \Fieldset ;
77use Statamic \Fields \Blueprint ;
88use Statamic \Fields \Field ;
99use Statamic \Support \Str ;
10+ use Facades \Statamic \Importer \Support \FieldUpdater as FieldUpdaterFacade ;
1011
1112class 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 ' ])
0 commit comments