Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter mjwlist

    (@mjwlist)

    Thanks again for the reply. Unfortunately passing the third argument to get_field as true throws an error with wp-graphql.

    In the end I’ve settled for a bit of a messy brute force approach but it’s working for now..

    Would be nice to see more acf addons working with wp-graphql in the future though.

    Thanks for the plugin none the less!!

    /**
    * Add custom field types to the list of supported field types in WPGraphQL for ACF.
    */
    add_filter('wpgraphql_acf_supported_fields', function ($supported_fields) {
    return array_merge($supported_fields, ['font_awesome', 'unique_id']);
    });

    add_filter('wpgraphql_acf_register_graphql_field', function ($field_config, $type_name, $field_name, $config) {
    $acf_field = $config['acf_field'] ?? null;

    if (!$acf_field || !in_array($acf_field['type'] ?? '', ['font-awesome', 'unique_id'], true)) {
    return $field_config;
    }

    // Define GraphQL field type and resolver function
    $field_config['type'] = 'String';

    // Custom resolver function
    $field_config['resolve'] = function ($root) use ($acf_field) {
    // Get the raw field value
    $value = isset($root->ID) ? get_field($acf_field['key'], $root->ID, false) : ($root[$acf_field['key']] ?? null);

    // Handle FontAwesome field type
    if ($acf_field['type'] === 'font-awesome' && is_string($value)) {
    return handle_fontawesome_value($value);
    }

    // Return the value as-is for other fields (e.g., 'unique_id')
    return $value ?: null;
    };

    return $field_config;
    }, 10, 4);

    /**
    * Handle FontAwesome value, whether it's a JSON string or plain string.
    * Because acf font awesome v5 and v6 return different values (either stringyfied object or simple string).
    *
    * @param string $value The raw value from the ACF field.
    * @return string|null The processed FontAwesome class string or null if invalid.
    */
    function handle_fontawesome_value($value)
    {
    // Check if the value looks like a JSON string (starts with "{" or "[")
    if ($value[0] === '{' || $value[0] === '[') {
    // Attempt to decode the JSON string
    $decoded_value = json_decode($value, true);

    // If decoding succeeds and it's an array, construct the FontAwesome class
    if (json_last_error() === JSON_ERROR_NONE && is_array($decoded_value)) {
    $style = $decoded_value['style'] ?? 'regular';
    $id = $decoded_value['id'] ?? '';

    return "fa-$style fa-$id"; // Return the formatted FontAwesome class string
    }
    }

    // If not JSON, assume it's already a valid Font Awesome class string
    return $value;
    }
    Thread Starter mjwlist

    (@mjwlist)

    /**
    * Add custom field types to the list of supported field types in WPGraphQL for ACF.
    */
    add_filter('wpgraphql_acf_supported_fields', function ($supported_fields) {
    return array_merge($supported_fields, ['font_awesome', 'unique_id']);
    });

    /**
    * Register custom ACF field types in the GraphQL schema.
    */
    add_filter('wpgraphql_acf_register_graphql_field', function ($field_config, $type_name, $field_name, $config) {
    $acf_field = $config['acf_field'] ?? null;

    if (!$acf_field || !in_array($acf_field['type'] ?? '', ['font-awesome', 'unique_id'], true)) {
    return $field_config;
    }

    // Define GraphQL field type and resolver function
    $field_config['type'] = 'String';
    $field_config['resolve'] = function ($root) use ($acf_field) {
    if (isset($root->ID)) {
    return get_field($acf_field['key'], $root->ID, false) ?: null;
    }
    return $root[$acf_field['key']] ?? null;
    };

    return $field_config;
    }, 10, 4);

    So I’ve been back to check what code we already have and the above is how we have been registering a couple of custom acf field types into the graphql schema. As you can see we are using get_field() in the resolver so I’m still at a loss as to why this isn’t returning the type as specified in the field settings.

    Do you have any ideas.. ??

    Thread Starter mjwlist

    (@mjwlist)

    Thanks for the response. I’ll look into the “register_graphql_acf_field_type” API and post any success going down that route.

Viewing 3 replies - 1 through 3 (of 3 total)