New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: add support for custom acf field types #11
Comments
I was actually really curious how to go about this as I've been able to get everything working except for a single custom ACF field I've built as a plugin. I imagine the ideal situation is to be able to register a custom acf field and its relevant field config in the plugin itself, but even just modifying the -- Edit: Ah of course, it's about using the acf |
@alancwoo Curious if you got this working? |
For anyone interested, I managed to enable a third-party field type using the following approach: /**
* Add field type to the list of supported fields types
*/
add_filter( 'wpgraphql_acf_supported_fields', function( $supported_fields ) {
$supported_fields[] = 'my_custom_field_type';
return $supported_fields;
} );
/**
* Handle the custom field type
* inspired by https://github.com/wp-graphql/wp-graphql-acf/blob/develop/src/class-config.php#L405-L427
*/
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 !== 'my_custom_field_type' ) {
return $field_config;
}
// define data type
$field_config['type'] = 'String';
// add resolver
$field_config['resolve'] = function( $root ) use ( $acf_field ) {
// when field is used in WP_Post and is top-level field (not nested in repeater, flexible content etc.)
if( $root->ID ) {
$value = get_field( $acf_field['key'], $root->ID, false );
// when field is used in WP_Post and is nested in repeater, flexible content etc. ...
} elseif( array_key_exists( $acf_field['key'], $root ) ) {
$value = $root[$acf_field['key']];
}
// TODO: handle other contexts like WP_Term, WP_User etc...
return ! empty( $value ) ? $value : null;
};
return $field_config;
}, 100, 4 ); |
@hatsumatsu Thanks for the snippet! I've found that it works well until you try to query the field in a repeater / flexible content. If you do, it simply returns |
@intelligence I actually do use this snippet in a quite complex flexible content field with all plugins' latest versions. Have you tried logging in-between-values in the filter function? |
@hatsumatsu Thanks! After some debugging I found that the code failed on This change seems to fix it for me: |
Closing as there's a solution above. The new version of WPGraphQL for ACF being worked on over here: https://github.com/wp-graphql/wpgraphql-acf also includes an API specifically for adding support for custom ACF Field Types. The new plugin ships support for most ACF Extended (Free and PRO) field types and you can see how they use the new APIs here: |
It would be great to have a filter in
\WPGraphQL\ACF\Config::add_field_group_fields
inside the foreach loop, so I can register a type for a custom ACF field type.Right now, there's a filter for
wpgraphql_acf_supported_fields
, so the custom field type can be "supported", but I don't see how to accomplish the next step without another filter inadd_field_group_fields
The text was updated successfully, but these errors were encountered: