Skip to content

fix: clone field resolver bug #254

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/FieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@
// 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']
);
}

Expand Down Expand Up @@ -365,17 +366,19 @@
$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'];
Expand All @@ -384,6 +387,9 @@
}
}

if ( empty( $field_key ) ) {
return null;
}

$should_format_value = false;

Expand All @@ -391,12 +397,18 @@
$should_format_value = true;
}

if ( empty( $field_key ) ) {

Check failure on line 400 in src/FieldConfig.php

View workflow job for this annotation

GitHub Actions / Check code (8.1)

Variable $field_key in empty() always exists and is not falsy.
return null;
}

// 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
Expand Down
16 changes: 13 additions & 3 deletions src/FieldType/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 0 additions & 33 deletions src/FieldType/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading