Created
May 3, 2023 22:13
-
-
Save jasonbahl/89acb832c38872085e050f473ad14e67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _graphql_acf_sanitize_flexible_content_resolver( $value, $acf_field, $context, $type_name ) { | |
if ( ! isset( $value['acf_fc_layout'] ) ) { | |
return null; | |
} | |
$type_registry = $context->type_registry; | |
$field_type_name = $type_name . '_' . ucfirst( \WPGraphQL\ACF\Config::camel_case( $acf_field['name'] ) ); | |
$layout_types = []; | |
if ( ! empty( $acf_field['layouts'] ) && is_array( $acf_field['layouts'] ) ) { | |
foreach ( $acf_field['layouts'] as $layout ) { | |
$flex_field_layout_name = ! empty( $layout['name'] ) ? ucfirst( \WPGraphQL\ACF\Config::camel_case( $layout['name'] ) ) : null; | |
$flex_field_layout_name = ! empty( $flex_field_layout_name ) ? $field_type_name . '_' . $flex_field_layout_name : null; | |
if ( empty( $flex_field_layout_name ) ) { | |
continue; | |
} | |
if ( ! $type_registry->get_type( $flex_field_layout_name ) ) { | |
continue; | |
} | |
$layout_types[ $layout['name'] ] = $flex_field_layout_name; | |
} | |
if ( array_key_exists( $value['acf_fc_layout'], $layout_types ) ) { | |
return $value; | |
} | |
} | |
return null; | |
} | |
add_filter( 'graphql_resolve_field', function( $default, $source, $args, \WPGraphQL\AppContext $context, \GraphQL\Type\Definition\ResolveInfo $info, $type_name, $field_key, $field, $field_resolver ) { | |
if ( ! isset( $info->fieldDefinition->config['acf_field']['type'] ) ) { | |
return $default; | |
} | |
if ( 'flexible_content' !== $info->fieldDefinition->config['acf_field']['type'] && 'flexible_content' !== $info->fieldDefinition->config['acf_field']['type'] ) { | |
return $default; | |
} | |
if ( empty( $default ) ) { | |
return $default; | |
} | |
if ( is_array( $default ) ) { | |
$sanitized = []; | |
foreach( $default as $value ) { | |
$sanitized[] = _graphql_acf_sanitize_flexible_content_resolver( $value, $info->fieldDefinition->config['acf_field'], $context, $info->parentType->name ); | |
} | |
return array_filter( $sanitized ); | |
} | |
return _graphql_acf_sanitize_flexible_content_resolver( $default, $info->fieldDefinition->config['acf_field'], $context, $info->parentType->name ); | |
}, 10, 9 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment