wp-graphql-acf icon indicating copy to clipboard operation
wp-graphql-acf copied to clipboard

Feature request: add support for custom acf field types

Open mlipscombe opened this issue 6 years ago • 6 comments

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 in add_field_group_fields

mlipscombe avatar Jun 21 '19 08:06 mlipscombe

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 wp-graphql-acf source itself, I'm not able to get this field to appear nor the Show in GraphQL to appear.

--

Edit: Ah of course, it's about using the acf name not label - so I'm now able to get the 'Show in GraphQL` to appear.

alancwoo avatar May 29 '20 10:05 alancwoo

@alancwoo Curious if you got this working? Is there any way to enable additional field types without modifying the plugin's source?

hatsumatsu avatar Feb 01 '21 10:02 hatsumatsu

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 avatar Feb 05 '21 09:02 hatsumatsu

@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 null. Maybe some update in ACF have broken it.

intelligence avatar Oct 11 '21 09:10 intelligence

@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 avatar Oct 11 '21 15:10 hatsumatsu

@hatsumatsu Thanks! After some debugging I found that the code failed on if($root->ID) { ... } as it's not an object when the field is nested within a repeater or flexible content. The else if { ... } never was evaluated.

This change seems to fix it for me: if( is_object($root) && $root->ID ) { ... }

intelligence avatar Oct 11 '21 20:10 intelligence

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:

  • https://github.com/wp-graphql/wpgraphql-acf/tree/main/src/ThirdParty/AcfExtended

jasonbahl avatar Jul 11 '23 17:07 jasonbahl