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

Fix post object #92

Open
wants to merge 8 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
99 changes: 53 additions & 46 deletions src/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,52 +490,59 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {
];
break;
case 'page_link':
case 'post_object':

if ( isset( $acf_field['post_type'] ) && is_array( $acf_field['post_type'] ) ) {
$field_type_name = $type_name . '_' . ucfirst( self::camel_case( $acf_field['name'] ) );
if ( $this->type_registry->get_type( $field_type_name ) == $field_type_name ) {
$type = $field_type_name;
} else {
$type_names = [];
foreach ( $acf_field['post_type'] as $post_type ) {
if ( in_array( $post_type, \WPGraphQL::get_allowed_post_types(), true ) ) {
$type_names[ $post_type ] = get_post_type_object( $post_type )->graphql_single_name;
}
}

if ( empty( $type_names ) ) {
$field_config['type'] = null;
break;
}

register_graphql_union_type( $field_type_name, [
'typeNames' => $type_names,
'resolveType' => function( $value ) use ( $type_names ) {
$post_type_object = get_post_type_object( $value->post_type );
return ! empty( $post_type_object->graphql_single_name ) ? $this->type_registry->get_type( $post_type_object->graphql_single_name ) : null;
}
] );

$type = $field_type_name;
}
} else {
$type = 'PostObjectUnion';
}

$field_config = [
'type' => $type,
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
if ( $value instanceof \WP_Post ) {
return new Post( $value );
}

return absint( $value ) ? DataSource::resolve_post_object( (int) $value, $context ) : null;

},
];
break;
case 'post_object':

if ( isset( $acf_field['post_type'] ) && is_array( $acf_field['post_type'] ) ) {
$field_type_name = $type_name . '_' . ucfirst( self::camel_case( $acf_field['name'] ) );
if ( $this->type_registry->get_type( $field_type_name ) == $field_type_name ) {
$type = $field_type_name;
} else {
$type_names = [];
foreach ( $acf_field['post_type'] as $post_type ) {
if ( in_array( $post_type, \WPGraphQL::get_allowed_post_types(), true ) ) {
$type_names[ $post_type ] = get_post_type_object( $post_type )->graphql_single_name;
}
}

if ( empty( $type_names ) ) {
$field_config['type'] = "PostObjectUnion";
$type = 'PostObjectUnion';

} else {

register_graphql_union_type(
$field_type_name,
[
'typeNames' => $type_names,
'resolveType' => function ($value) use ($type_names) {
$post_type_object = get_post_type_object($value->post_type);
return !empty($post_type_object->graphql_single_name) ? $this->type_registry->get_type(
$post_type_object->graphql_single_name
) : null;
}
]
);

$type = $field_type_name;
}
}
} else {
$type = 'PostObjectUnion';
}

$field_config = [
'type' => $type,
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
if ( $value instanceof \WP_Post ) {
return new Post( $value );
}

return absint( $value ) ? DataSource::resolve_post_object( (int) $value, $context ) : null;

},
];
break;
case 'link':

$field_type_name = 'ACF_Link';
Expand Down
7 changes: 0 additions & 7 deletions vendor/autoload_commands.php

This file was deleted.

7 changes: 0 additions & 7 deletions vendor/autoload_framework.php

This file was deleted.

1 change: 0 additions & 1 deletion vendor/composer/installed.json

This file was deleted.

2 changes: 1 addition & 1 deletion wp-graphql-acf.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Plugin Name: WPGraphQL for Advanced Custom Fields
* Plugin Name: WPGraphQL for Advanced Custom Fields - YT Advisors
* Plugin URI: https://wpgraphql.com/acf
* Description: Adds Advanced Custom Fields to the WPGraphQL Schema
* Author: WPGraphQL, Jason Bahl
Expand Down