Skip to content

Commit ece620b

Browse files
authored
Merge pull request #185 from wp-graphql/fix/184-clone-fields-in-groups-return-null
fix: clone field within a group field type returns null values
2 parents b06cf28 + 923ae22 commit ece620b

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

src/FieldConfig.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,17 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI
401401

402402
// If the root being passed down already has a value
403403
// for the field key, let's use it to resolve
404-
if ( ! empty( $root[ $field_key ] ) ) {
405-
return $this->prepare_acf_field_value( $root[ $field_key ], $node, $node_id, $field_config );
404+
if ( isset( $field_config['key'] ) && ! empty( $root[ $field_config['key'] ] ) ) {
405+
return $this->prepare_acf_field_value( $root[ $field_config['key'] ], $node, $node_id, $field_config );
406406
}
407407

408-
if ( ! empty( $root[ $field_config['name'] ] ) ) {
408+
// Check if the cloned field key is being used to pass values down
409+
if ( isset( $field_config['__key'] ) && ! empty( $root[ $field_config['__key'] ] ) ) {
410+
return $this->prepare_acf_field_value( $root[ $field_config['__key'] ], $node, $node_id, $field_config );
411+
}
412+
413+
// Else check if the values are being passed down via the name
414+
if ( isset( $field_config['name'] ) && ! empty( $root[ $field_config['name'] ] ) ) {
409415
return $this->prepare_acf_field_value( $root[ $field_config['name'] ], $node, $node_id, $field_config );
410416
}
411417

tests/wpunit/FieldTypes/GroupFieldTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,122 @@ public function get_expected_block_fragment_response() {
9898
];
9999
}
100100

101+
/**
102+
* @see: https://github.com/wp-graphql/wpgraphql-acf/issues/184
103+
* @see: https://github.com/wp-graphql/wpgraphql-acf/issues/151
104+
* @return void
105+
*/
106+
public function testCloneGroupFieldResolvesDataAsExpected() {
107+
108+
// if ACF PRO is not active, skip the test
109+
if ( ! defined( 'ACF_PRO' ) ) {
110+
$this->markTestSkipped( 'ACF Pro is not active so this test will not run.' );
111+
}
112+
113+
$field_key = $this->register_acf_field([
114+
'key' => 'field_65df9360bebef',
115+
'label' => 'My Text Field',
116+
'name' => 'my_text_field',
117+
'type' => 'text',
118+
'show_in_graphql' => 1,
119+
'graphql_field_name' => 'myTextField',
120+
'graphql_non_null' => 0,
121+
], [
122+
'key' => 'group_65df9360afcb0',
123+
'graphql_field_name' => 'myCollectionOfFieldTypes',
124+
'show_in_graphql' => 1,
125+
'map_graphql_types_from_location_rules' => 0,
126+
'graphql_types' => '',
127+
'location' => [
128+
[
129+
[
130+
'param' => 'post_type',
131+
'operator' => '==',
132+
'value' => 'post',
133+
],
134+
],
135+
],
136+
'active' => true,
137+
]);
138+
139+
140+
$cloned_field_key = $this->register_acf_field([
141+
'label' => 'My Group Field',
142+
'name' => 'my_group_field',
143+
'show_in_graphql' => 1,
144+
'type' => 'group',
145+
'graphql_field_name' => 'myGroupField',
146+
'graphql_non_null' => 0,
147+
'sub_fields' => [
148+
[
149+
'key' => 'field_65e1ff262ce23',
150+
'label'=> 'Clone of My Collection of Field Types (no prefix)',
151+
'name' => 'clone_of_my_collection_of_field_types_no_prefix',
152+
'type' => 'clone',
153+
'graphql_field_name' => 'cloneOfMyCollectionOfFieldTypesNoPrefix',
154+
'clone' => [
155+
'group_65df9360afcb0'
156+
],
157+
'prefix_name' => 0,
158+
]
159+
]
160+
], [
161+
'key' => 'group_65e1feb751e7d',
162+
'title' => 'My Group with Clone',
163+
'location' => [
164+
[
165+
[
166+
'param' => 'page_template',
167+
'operator' => '==',
168+
'value' => 'default',
169+
],
170+
],
171+
],
172+
'graphql_field_name' => 'myGroupWithClone',
173+
'show_in_graphql' => 1,
174+
'graphql_types' => [ 'DefaultTemplate' ],
175+
'active' => true,
176+
]);
177+
178+
$query = '
179+
query NewQuery($id:ID!) {
180+
page(id: $id, idType: DATABASE_ID) {
181+
databaseId
182+
template {
183+
... on DefaultTemplate {
184+
myGroupWithClone {
185+
myGroupField {
186+
myTextField
187+
}
188+
}
189+
}
190+
}
191+
}
192+
}
193+
';
194+
195+
// save text value to the cloned field's meta key
196+
$expected_text_value = 'text field value...';
197+
update_field( 'my_group_field_my_text_field', $expected_text_value, $this->published_page->ID );
198+
199+
$actual = $this->graphql([
200+
'query' => $query,
201+
'variables' => [
202+
'id' => $this->published_page->ID,
203+
]
204+
]);
205+
206+
207+
208+
codecept_debug( [
209+
'$actual' => $actual,
210+
]);
211+
212+
self::assertQuerySuccessful( $actual, [
213+
$this->expectedField( 'page.databaseId', $this->published_page->ID ),
214+
$this->expectedField( 'page.template.myGroupWithClone.myGroupField.myTextField', $expected_text_value ),
215+
]);
216+
217+
}
218+
101219
}

0 commit comments

Comments
 (0)