Skip to content
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

Closed
mlipscombe opened this issue Jun 21, 2019 · 7 comments
Closed

Feature request: add support for custom acf field types #11

mlipscombe opened this issue Jun 21, 2019 · 7 comments

Comments

@mlipscombe
Copy link

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

@jasonbahl jasonbahl added this to Prioritized in 4: Actionable Issues Aug 7, 2019
@jasonbahl jasonbahl moved this from Prioritized to To Prioritize in 4: Actionable Issues Aug 7, 2019
@alancwoo
Copy link

alancwoo commented May 29, 2020

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.

@hatsumatsu
Copy link

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

@hatsumatsu
Copy link

hatsumatsu commented Feb 5, 2021

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 );

@intelligence
Copy link

@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.

@hatsumatsu
Copy link

@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?

@intelligence
Copy link

intelligence commented Oct 11, 2021

@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 ) { ... }

@jasonbahl
Copy link
Contributor

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants