diff --git a/src/FieldConfig.php b/src/FieldConfig.php index 624e85a..a30309d 100644 --- a/src/FieldConfig.php +++ b/src/FieldConfig.php @@ -148,9 +148,10 @@ public function get_field_description(): string { // translators: %s is the name of the ACF Field Group $description = sprintf( // translators: %1$s is the ACF Field Type and %2$s is the name of the ACF Field Group - __( 'Field of the "%1$s" Field Type added to the schema as part of the "%2$s" Field Group', 'wpgraphql-acf' ), + __( 'Field of the "%1$s" Field Type added to the schema as part of the "%2$s" Field Group (%3$s)', 'wpgraphql-acf' ), $this->acf_field['type'] ?? '', - $this->registry->get_field_group_graphql_type_name( $this->acf_field_group ) + $this->registry->get_field_group_graphql_type_name( $this->acf_field_group ), + $this->acf_field_group['key'] ); } @@ -365,17 +366,19 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI $field_key = null; $is_cloned = false; - if ( ! empty( $field_config['__key'] ) ) { + + + if ( ! empty( $root['sub_field_keys'] ) && is_array( $root['sub_field_keys'] ) ) { + if ( isset( $root['sub_field_keys'][ $field_config['key'] ] ) ) { + $field_key = $root['sub_field_keys'][ $field_config['key'] ]; + } + } else if ( ! empty( $field_config['__key'] ) ) { $field_key = $field_config['__key']; $is_cloned = true; } elseif ( ! empty( $field_config['key'] ) ) { $field_key = $field_config['key']; } - if ( empty( $field_key ) ) { - return null; - } - if ( $is_cloned ) { if ( isset( $field_config['_name'] ) && ! empty( $node_id ) ) { $field_key = $field_config['_name']; @@ -384,6 +387,9 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI } } + if ( empty( $field_key ) ) { + return null; + } $should_format_value = false; @@ -397,6 +403,12 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI // if the field_config is empty or not an array, set it as an empty array as a fallback $field_config = ! empty( $field_config ) ? $field_config : []; + + $root = $root['value'] ?? $root; + + if ( ! empty( $root[ $field_key ] ) ) { + return $this->prepare_acf_field_value( $root[ $field_key ], $node, $node_id, $field_config ); + } // If the root being passed down already has a value // for the field key, let's use it to resolve diff --git a/src/FieldType/Group.php b/src/FieldType/Group.php index 51698f8..95d42ea 100644 --- a/src/FieldType/Group.php +++ b/src/FieldType/Group.php @@ -16,7 +16,7 @@ public static function register_field_type(): void { 'group', [ 'graphql_type' => static function ( FieldConfig $field_config, AcfGraphQLFieldType $acf_field_type ) { - $sub_field_group = $field_config->get_acf_field(); + $sub_field_group = $field_config->get_raw_acf_field(); $parent_type = $field_config->get_parent_graphql_type_name( $sub_field_group ); $field_name = $field_config->get_graphql_field_name(); @@ -25,6 +25,7 @@ public static function register_field_type(): void { $sub_field_group['graphql_type_name'] = $type_name; $sub_field_group['graphql_field_name'] = $type_name; $sub_field_group['parent'] = $sub_field_group['key']; + $sub_field_group['fields'] = ! empty( $sub_field_group['sub_fields'] ) ? $sub_field_group['sub_fields'] : acf_get_raw_fields( $sub_field_group['ID'] ); // Determine if the group is a clone field $cloned_type = null; @@ -88,13 +89,22 @@ public static function register_field_type(): void { return $type_name; }, 'resolve' => static function ( $root, $args, AppContext $context, $info, $field_type, FieldConfig $field_config ) { + + $value = $field_config->resolve_field( $root, $args, $context, $info ); - if ( ! empty( $value ) ) { - return $value; + + + $sub_field_keys = []; + if ( ! empty( $info->fieldDefinition->config['acf_field']['sub_fields'] ) ) { + foreach ( $info->fieldDefinition->config['acf_field']['sub_fields'] as $sub_field ) { + $key = $sub_field['__key'] ?? $sub_field['key']; + $sub_field_keys[ $key ] = $sub_field['key']; + } } $root['value'] = $value; + $root['sub_field_keys'] = $sub_field_keys; $root['acf_field_group'] = $field_config->get_acf_field(); return $root; diff --git a/src/FieldType/Repeater.php b/src/FieldType/Repeater.php index bb9b8d6..88aaa77 100644 --- a/src/FieldType/Repeater.php +++ b/src/FieldType/Repeater.php @@ -24,39 +24,6 @@ public static function register_field_type(): void { $sub_field_group['graphql_field_name'] = $type_name; $sub_field_group['locations'] = null; - // Determine if the group is a clone field - $cloned_type = null; - - // if the field group is actually a cloned field group, we - // can return the GraphQL Type of the cloned field group - if ( isset( $sub_field_group['_clone'] ) ) { - $cloned_from = acf_get_field( $sub_field_group['_clone'] ); - - if ( ! empty( $cloned_from['clone'] ) && is_array( $cloned_from['clone'] ) ) { - foreach ( $cloned_from['clone'] as $clone_field ) { - $cloned_group = acf_get_field_group( $clone_field ); - - if ( ! $cloned_group ) { - continue; - } - - if ( ! $field_config->get_registry()->should_field_group_show_in_graphql( $cloned_group ) ) { - continue; - } - - $cloned_type = $field_config->get_registry()->get_field_group_graphql_type_name( $cloned_group ); - break; - } - } - } - - // If the group is a clone field, return the cloned type instead of registering - // another Type in the registry - if ( $cloned_type ) { - return [ 'list_of' => Utils::format_type_name( $cloned_type . ' ' . $field_name ) ]; - } - - $field_config->get_registry()->register_acf_field_groups_to_graphql( [ $sub_field_group,