Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbahl/89acb832c38872085e050f473ad14e67 to your computer and use it in GitHub Desktop.
Save jasonbahl/89acb832c38872085e050f473ad14e67 to your computer and use it in GitHub Desktop.
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