Skip to content

Commit 65a2fa1

Browse files
authored
feat: Replace GfFieldWithProductFieldSetting.productField with .connectedProductField. (#435)
* feat: Replace `GfFieldWithProductFieldSetting.productField` for `.connectedProductField`. * chore: cleanup
1 parent 181ff20 commit 65a2fa1

File tree

9 files changed

+49
-12
lines changed

9 files changed

+49
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- fix: Add missing descriptions to types.
1414
- feat: Add `FieldError.connectedFormField` connection to `FieldError` type.
1515
- feat: Add support for WPGraphQL Content Blocks.
16+
- feat: Add `GfFieldWithProductFieldSetting.connectedProductField` connection and deprecate `.productField` field.
1617
- dev: Remove `vendor` directory from the GitHub repository.
1718
- dev: Use `FormFieldsDataLoader` to resolve fields instead of instantiating a new `Model`.
1819
- dev: use WP_Filesystem to handle Signature field uploads.

src/Registry/FormFieldRegistry.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ static function ( TypeRegistry $type_registry ) use ( $field, $interface_setting
161161

162162
$config['resolveType'] = static function ( $value ) use ( $type_registry, $possible_types ) {
163163
$input_type = $value->get_input_type();
164+
164165
if ( isset( $possible_types[ $input_type ] ) ) {
165-
$type = $type_registry->get_type( $possible_types[ $value->$input_type ] );
166+
$type = $type_registry->get_type( $possible_types[ $input_type ] );
167+
166168
if ( null !== $type ) {
167169
return $type;
168170
}
@@ -173,7 +175,7 @@ static function ( TypeRegistry $type_registry ) use ( $field, $interface_setting
173175
/* translators: %s: GF field type */
174176
esc_html__( 'The "%1$1s" Gravity Forms field does not yet support the %2$2s input type.', 'wp-graphql-gravity-forms' ),
175177
esc_html( $value->type ),
176-
esc_html( $value->inputType ),
178+
esc_html( $input_type ?? $value->inputType ),
177179
)
178180
);
179181
};

src/Type/WPInterface/FieldSetting/FieldWithProductField.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
namespace WPGraphQL\GF\Type\WPInterface\FieldSetting;
1212

13+
use WPGraphQL\AppContext;
14+
use WPGraphQL\GF\Data\Loader\FormFieldsLoader;
15+
1316
/**
1417
* Class - FieldWithProductField
1518
*/
@@ -32,11 +35,24 @@ class FieldWithProductField extends AbstractFieldSetting {
3235
* {@inheritDoc}
3336
*/
3437
public static function get_fields(): array {
35-
// @todo make connection.
3638
return [
37-
'productField' => [
38-
'type' => 'Int',
39-
'description' => __( 'The id of the product field to which the field is associated.', 'wp-graphql-gravity-forms' ),
39+
'productField' => [
40+
'type' => 'Int',
41+
'description' => __( 'The id of the product field to which the field is associated.', 'wp-graphql-gravity-forms' ),
42+
'deprecationReason' => __( 'Use `connectedProductField` field instead.', 'wp-graphql-gravity-forms' ),
43+
],
44+
'connectedProductField' => [
45+
'type' => 'ProductField',
46+
'description' => __( 'The product field to which the field is associated.', 'wp-graphql-gravity-forms' ),
47+
'resolve' => static function ( $source, array $args, AppContext $context ) {
48+
if ( ! isset( $context->gfForm ) || empty( $source->productField ) ) {
49+
return null;
50+
}
51+
52+
$id_for_loader = FormFieldsLoader::prepare_loader_id( $context->gfForm->databaseId, (int) $source->productField );
53+
54+
return $context->get_loader( FormFieldsLoader::$name )->load_deferred( $id_for_loader );
55+
},
4056
],
4157
];
4258
}

tests/wpunit/OptionCheckboxFieldTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,10 @@ public function field_query(): string {
294294
shouldExport
295295
}
296296
placeholder
297-
productField
297+
connectedProductField {
298+
databaseId
299+
type
300+
}
298301
type
299302
... on OptionCheckboxField {
300303
checkboxValues {

tests/wpunit/OptionRadioFieldTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ public function field_query(): string {
177177
shouldExport
178178
}
179179
placeholder
180-
productField
180+
connectedProductField {
181+
databaseId
182+
type
183+
}
181184
type
182185
... on OptionRadioField {
183186
hasOtherChoice

tests/wpunit/OptionSelectFieldTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ public function field_query(): string {
184184
shouldExport
185185
}
186186
placeholder
187-
productField
187+
connectedProductField {
188+
databaseId
189+
type
190+
}
188191
type
189192
... on OptionSelectField {
190193
autocompleteAttribute

tests/wpunit/QuantityHiddenFieldTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ public function field_query(): string {
164164
shouldExport
165165
}
166166
placeholder
167-
productField
167+
connectedProductField {
168+
databaseId
169+
type
170+
}
168171
type
169172
... on QuantityHiddenField {
170173
value

tests/wpunit/QuantityNumberFieldTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ public function field_query(): string {
171171
shouldExport
172172
}
173173
placeholder
174-
productField
174+
connectedProductField {
175+
databaseId
176+
type
177+
}
175178
type
176179
value
177180
... on QuantityNumberField {

tests/wpunit/QuantitySelectFieldTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ public function field_query(): string {
199199
shouldExport
200200
}
201201
placeholder
202-
productField
202+
connectedProductField {
203+
databaseId
204+
type
205+
}
203206
type
204207
value
205208
... on QuantitySelectField {

0 commit comments

Comments
 (0)