@@ -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 ,
0 commit comments