Skip to content

Commit 70974d6

Browse files
committed
- simplify duplicate logic in Clone Field
1 parent 0ea2f27 commit 70974d6

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/FieldType/CloneField.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,8 @@ static function ( $cloned ) use ( $field_config ) {
4848
if ( ! $prefix_name ) {
4949
register_graphql_interfaces_to_types( $cloned_group_interfaces, [ $parent_type ] );
5050
} else {
51-
$sub_field_group['graphql_type_name'] = $type_name;
52-
$sub_field_group['graphql_field_name'] = $type_name;
53-
$sub_field_group['parent'] = $sub_field_group['key'];
54-
$sub_field_group['sub_fields'] = $cloned_fields;
55-
$field_config->get_registry()->register_acf_field_groups_to_graphql(
56-
[
57-
$sub_field_group,
58-
]
59-
);
60-
51+
$type_name = self::register_prefixed_clone_field_type( $type_name, $sub_field_group, $cloned_fields, $field_config );
6152
register_graphql_interfaces_to_types( $cloned_group_interfaces, [ $type_name ] );
62-
6353
return $type_name;
6454
}
6555
}
@@ -83,17 +73,7 @@ static function ( $cloned ) use ( $field_config ) {
8373
// Register a new Object Type with the cloned fields, and return
8474
// the new type.
8575
} else {
86-
$sub_field_group['graphql_type_name'] = $type_name;
87-
$sub_field_group['graphql_field_name'] = $type_name;
88-
$sub_field_group['parent'] = $sub_field_group['key'];
89-
$sub_field_group['sub_fields'] = $cloned_fields;
90-
91-
$field_config->get_registry()->register_acf_field_groups_to_graphql(
92-
[
93-
$sub_field_group,
94-
]
95-
);
96-
return $type_name;
76+
return self::register_prefixed_clone_field_type( $type_name, $sub_field_group, $cloned_fields, $field_config );
9777
}
9878
}
9979
// Bail by returning a NULL type
@@ -115,4 +95,27 @@ static function ( $cloned ) use ( $field_config ) {
11595
]
11696
);
11797
}
98+
99+
/**
100+
* @param string $type_name The name of the GraphQL Type representing the prefixed clone field
101+
* @param array $sub_field_group The Field Group representing the cloned field
102+
* @param array $cloned_fields The cloned fields to be registered to the Cloned Field Type
103+
* @param \WPGraphQL\Acf\FieldConfig $field_config The ACF Field Config
104+
*
105+
* @return string
106+
* @throws \Exception
107+
*/
108+
public static function register_prefixed_clone_field_type( string $type_name, array $sub_field_group, array $cloned_fields, FieldConfig $field_config ): string {
109+
$sub_field_group['graphql_type_name'] = $type_name;
110+
$sub_field_group['graphql_field_name'] = $type_name;
111+
$sub_field_group['parent'] = $sub_field_group['key'];
112+
$sub_field_group['sub_fields'] = $cloned_fields;
113+
114+
$field_config->get_registry()->register_acf_field_groups_to_graphql(
115+
[
116+
$sub_field_group,
117+
]
118+
);
119+
return $type_name;
120+
}
118121
}

src/FieldType/FlexibleContent.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public static function register_field_type(): void {
5252

5353
// If there are no layouts, return a NULL type
5454
if ( ! empty( $acf_field['layouts'] ) ) {
55-
5655
foreach ( $acf_field['layouts'] as $layout ) {
5756
$layout_type_name = Utils::format_type_name( $layout_interface_prefix . ' ' . $field_config->get_registry()->get_field_group_graphql_type_name( $layout ) ) . 'Layout';
5857
$layout['interfaces'] = [ $layout_interface_name ];
@@ -63,7 +62,7 @@ public static function register_field_type(): void {
6362

6463
$sub_fields = array_filter(
6564
array_map(
66-
static function( $field ) use ( $layout ) {
65+
static function ( $field ) use ( $layout ) {
6766
$field['graphql_types'] = [];
6867
$field['parent_layout_group'] = $layout;
6968
$field['isFlexLayoutField'] = true;
@@ -78,7 +77,6 @@ static function( $field ) use ( $layout ) {
7877

7978
$layouts[] = $layout;
8079
}
81-
8280
}
8381

8482
if ( ! empty( $layouts ) ) {

0 commit comments

Comments
 (0)