33
44use WPGraphQL \Acf \AcfGraphQLFieldType ;
55use WPGraphQL \Acf \FieldConfig ;
6- use WPGraphQL \AppContext ;
76use WPGraphQL \Utils \Utils ;
87
98class CloneField {
@@ -16,8 +15,7 @@ public static function register_field_type():void {
1615 'clone ' ,
1716 [
1817 'graphql_type ' => static function ( FieldConfig $ field_config , AcfGraphQLFieldType $ acf_field_type ) {
19-
20- $ sub_field_group = $ field_config ->get_acf_field ();
18+ $ sub_field_group = $ field_config ->get_raw_acf_field ();
2119 $ parent_type = $ field_config ->get_parent_graphql_type_name ( $ sub_field_group );
2220 $ field_name = $ field_config ->get_graphql_field_name ();
2321
@@ -35,29 +33,48 @@ public static function register_field_type():void {
3533 }
3634 }
3735
38- if ( ! empty ( $ cloned_groups ) && false === ( bool ) $ sub_field_group [ ' prefix_name ' ] ) {
39- $ parent_group = acf_get_field_group ( $ sub_field_group [ ' parent ' ] );
40- $ cloned_group_interfaces = [];
41- foreach ( $ cloned_groups as $ cloned_group ) {
36+ $ cloned_group_interfaces = [];
37+
38+ if ( ! empty ( $ cloned_groups ) ) {
39+ foreach ( $ cloned_groups as $ cloned_group ) {
4240 $ cloned_group_interfaces [] = $ field_config ->get_registry ()->get_field_group_graphql_type_name ( $ cloned_group ) . '_Fields ' ;
4341 }
44- // wp_send_json( [
45- // '$clone' => $sub_field_group['clone'],
46- // '$cloned_groups' => $cloned_groups,
47- // '$cloned_group_interfaces' => $cloned_group_interfaces,
48- // 'parent_field_group' => $parent_group,
49- // '$sub_field_group' => $sub_field_group,
50- // '$parent_graphql_type_name' => $parent_group_type_name
51- // ] );
52-
5342 }
5443
55- if ( ! empty ( $ cloned_group_interfaces ) && ! empty ( $ parent_group ) ) {
56- $ parent_group_type_name = $ field_config ->get_registry ()->get_field_group_graphql_type_name ( $ parent_group );
57- register_graphql_interfaces_to_types ( $ cloned_group_interfaces , [ $ parent_group_type_name ] );
58- return 'connection ' ;
44+ if ( ! empty ( $ cloned_group_interfaces ) ) {
45+
46+ // If a clone field clones all fields from another field group,
47+ // but has "prefix_name" false, implement the Interface on the parent group
48+ if ( false === (bool ) $ sub_field_group ['prefix_name ' ] ) {
49+ $ parent_group = acf_get_field_group ( $ sub_field_group ['parent ' ] );
50+
51+ if ( empty ( $ parent_group ) ) {
52+ $ parent_field = acf_get_field ( $ sub_field_group ['parent ' ] );
53+ $ parent_group = ! empty ( $ parent_field ) ? acf_get_field_group ( $ parent_field ['parent ' ] ) : false ;
54+ }
55+
56+ if ( ! empty ( $ parent_group ) ) {
57+ $ parent_group_type_name = $ field_config ->get_registry ()->get_field_group_graphql_type_name ( $ parent_group );
58+
59+ if ( isset ( $ sub_field_group ['isFlexLayoutField ' ] ) && true === (bool ) $ sub_field_group ['isFlexLayoutField ' ] ) {
60+ $ parent_type_name = $ field_config ->get_registry ()->get_field_group_graphql_type_name ( $ sub_field_group ['parent_layout_group ' ] ) ?? $ type_name ;
61+ register_graphql_interfaces_to_types ( $ cloned_group_interfaces , [ $ parent_type_name ] );
62+ } else {
63+ register_graphql_interfaces_to_types ( $ cloned_group_interfaces , [ $ parent_group_type_name ] );
64+ }
65+ return 'connection ' ;
66+ }
67+ // If "prefix_name" is true, nest the cloned field group within another GraphQL object type to avoid
68+ // collisions with multiple instances of the field group being cloned
69+ } else {
70+ if ( ! empty ( $ type_name ) ) {
71+ // Register the cloned group interfaces to the type representing the cloned fields
72+ register_graphql_interfaces_to_types ( $ cloned_group_interfaces , [ $ type_name ] );
73+ }
74+ }
5975 }
6076
77+
6178 $ sub_field_group ['graphql_type_name ' ] = $ type_name ;
6279 $ sub_field_group ['graphql_field_name ' ] = $ type_name ;
6380 $ sub_field_group ['parent ' ] = $ sub_field_group ['key ' ];
@@ -70,12 +87,6 @@ public static function register_field_type():void {
7087
7188 return $ type_name ;
7289 },
73- 'resolve ' => static function ( $ root , $ args , AppContext $ context , $ info , $ field_type , FieldConfig $ field_config ) {
74- $ value = $ field_config ->resolve_field ( $ root , $ args , $ context , $ info );
75- $ root ['value ' ] = $ value ;
76- $ root ['acf_field_group ' ] = $ field_config ->get_acf_field_group ();
77- return $ root ;
78- },
7990 // The clone field adds its own settings field to display
8091 'admin_fields ' => static function ( $ default_admin_settings , $ field , $ config , \WPGraphQL \Acf \Admin \Settings $ settings ) {
8192
0 commit comments