-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-graphql-acf-gf-picker.php
62 lines (37 loc) · 1.36 KB
/
wp-graphql-acf-gf-picker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* Plugin Name: GF ACF Block Form Picker
*/
if (!defined('ABSPATH')) {
exit;
}
add_filter('wpgraphql_acf_supported_fields', function ($supported_fields) {
$supported_fields[] = 'forms';
return $supported_fields;
});
add_filter('wpgraphql_acf_register_graphql_field', function ($field_config, $type_name, $field_name, $config) {
$acf_field = isset($config['acf_field']) ? $config['acf_field'] : null;
$acf_type = isset($acf_field['type']) ? $acf_field['type'] : null;
if (!$acf_field) {
return $field_config;
}
// ignore all other field types
if ($acf_type !== 'forms') {
return $field_config;
}
// define data type
$field_config['type'] = 'GfForm';
// add resolver
$field_config['resolve'] = function ($root, $args, \WPGraphQL\AppContext $context, $info) use ($acf_field) {
// wp_send_json( [ $root ] );
// Ideally use a more dynamic value from $acf_field here..
if ( isset ( $root['attributes']['data']["gf_acf_picker"] ) ) {
$value = $root['attributes']['data']["gf_acf_picker"];
}
if (!empty($value)) {
$form = $context->get_loader(\WPGraphQL\GF\Data\Loader\FormsLoader::$name)->load_deferred($value);
}
return !empty($form) ? $form : null;
};
return $field_config;
}, 10, 4);