Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Fix: relationship field should use graphql_acf_post_object_source filter #297

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
34 changes: 25 additions & 9 deletions src/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,6 @@ protected function register_graphql_field( string $type_name, string $field_name

$type = $field_type_name;
}


}
} else {
$type = 'PostObjectUnion';
Expand All @@ -681,20 +679,37 @@ protected function register_graphql_field( string $type_name, string $field_name
$field_config = [
'type' => [ 'list_of' => $type ],
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$relationship = [];
$value = $this->get_acf_field_value( $root, $acf_field );

$relationship = [];

if ( ! empty( $value ) && is_array( $value ) ) {
foreach ( $value as $post_id ) {
$post_object = get_post( $post_id );
if ( $post_object instanceof \WP_Post ) {
$post_model = new Post( $post_object );
$relationship[] = $post_model;
foreach ( $value as $id ) {
$post = get_post( $id );
if ( ! empty ( $post ) ) {
$relationship[] = new Post( $post );
}
}
}

return isset( $value ) ? $relationship : null;
$relationship = isset ( $value ) ? $relationship : null;

/**
* This hooks allows for filtering of the post object source. In case an non-core defined
* post-type is being targeted.
*
* @param mixed|null $source GraphQL Type source.
* @param mixed|null $value Root ACF field value.
* @param AppContext $context AppContext instance.
* @param ResolveInfo $info ResolveInfo instance.
*/
return apply_filters(
'graphql_acf_post_object_source',
$relationship,
$value,
$context,
$info
);

},
];
Expand Down Expand Up @@ -744,6 +759,7 @@ protected function register_graphql_field( string $type_name, string $field_name
$value = $this->get_acf_field_value( $root, $acf_field );

$return = [];

if ( ! empty( $value ) ) {
if ( is_array( $value ) ) {
foreach ( $value as $id ) {
Expand Down