wp-graphql-acf icon indicating copy to clipboard operation
wp-graphql-acf copied to clipboard

Not works with Acf crop image plugin

Open dinho-afsn opened this issue 6 years ago • 5 comments

I try use https://github.com/andersthorborg/ACF-Image-Crop . but not show in graphql.

dinho-afsn avatar Oct 06 '19 16:10 dinho-afsn

I would love for this plugin to work with wpgraph-ql. Any progress on this happening any time soon?

jdiponziano avatar Dec 10 '19 21:12 jdiponziano

I included the field 'image_crop' slug in the class-config.php

public static function get_supported_fields() {
	$supported_fields = [
		'text',
		'textarea',
		'number',
		'range',
		'email',
		'url',
		'password',
		'image',
		'image_crop',
		'file',
		'wysiwyg',
		'oembed',
		'gallery',
		'select',
		'checkbox',
		'radio',
		'button_group',
		'true_false',
		'link',
		'post_object',
		'page_link',
		'relationship',
		'taxonomy',
		'user',
		'google_map',
		'date_picker',
		'date_time_picker',
		'time_picker',
		'color_picker',
		'group',
		'repeater',
		'flexible_content'
	];

but I don't remember if it worked. I'll install the project again here and see if it's working

dinho-afsn avatar Dec 10 '19 21:12 dinho-afsn

Any custom extensions of ACF would need to be mapped to the WPGraphQL Schema. There's no programmatic way (that I can think of right now) to automatically have custom ACF Extensions work in the GraphQL Schema.

I'm open to suggestions if someone has an idea on how to get custom extensions to work automatically.

jasonbahl avatar Dec 10 '19 21:12 jasonbahl

If anyone has this problem still and needs a quick fix you can add 'image_aspect_ratio_crop' into the $supported_fields array (mentioned above by @dinho-afsn ) in /src/class-config.php. In the same file further down in a switch statement you also need to include it there where the 'image' and 'file' case are set. This would get removed though if you update the plugin.

Thanks for the plugin @jasonbahl and if a solution was found to add this automatically that would be awesome!

OliverBurns avatar Jan 27 '20 11:01 OliverBurns

Good news, I have added support for this plugin in the latest version of ACF Image Aspect Ratio Crop plugin. I’m not sure if the implementation is 100% correct but it seemed to work fine in my testing.

It would be nice to have some kind of documentation how to implement support for this plugin in a 3rd party ACF field.

joppuyo avatar Jul 28 '20 22:07 joppuyo

šŸ‘‹šŸ» We're re-building this plugin over here: https://github.com/wp-graphql/wpgraphql-acf and in the new version we have a new API for mapping ACF Field Types to the GraphQL Schema.

The new version of the plugin has built-in support for ACF Extended (free and PRO), which uses the new API for mapping custom ACF Field Types to the GraphQL Schema.

It's not fully documented yet, but you can see plenty of examples of using the new register_graphql_acf_field_type() API in this directory: https://github.com/wp-graphql/wpgraphql-acf/tree/main/src/ThirdParty/AcfExtended

Here's also a GIST showing how @CesarBenavides777 mapped the ACF Table field type to the GraphQL Schema: https://gist.github.com/CesarBenavides777/79b158db8355ddbc349fbf9b3272ae47

jasonbahl avatar Oct 31 '23 15:10 jasonbahl

If anyone has this problem still and needs a quick fix you can add 'image_aspect_ratio_crop' into the $supported_fields array (mentioned above by @dinho-afsn ) in /src/class-config.php. In the same file further down in a switch statement you also need to include it there where the 'image' and 'file' case are set. This would get removed though if you update the plugin.

Thanks for the plugin @jasonbahl and if a solution was found to add this automatically that would be awesome!

Where actually should this be updated?

flyingcodeeurope avatar Jan 24 '24 04:01 flyingcodeeurope

@flyingcodeeurope I'm not sure I follow?

Are you using the new version of WPGraphQL for ACF v2.0?

If so you can use the register_graphql_acf_field_type() as described above or in this guide: https://acf.wpgraphql.com/adding-support-for-3rd-party-acf-field-type/

You can put the code wherever you maintain custom code. Typically the functions.php of a child theme or a custom plugin.

jasonbahl avatar Jan 24 '24 04:01 jasonbahl

@jasonbahl I am looking for a way to use the ACF Image Crop in Grapql, but did not understand where to put the above code. Might be old? In this guide, https://acf.wpgraphql.com/adding-support-for-3rd-party-acf-field-type/ how do I add it?

flyingcodeeurope avatar Jan 24 '24 04:01 flyingcodeeurope

Ya you'll need to add the code wherever you maintain custom code.

Typically in WordPress you would use either a child theme or a custom plugin.

jasonbahl avatar Jan 24 '24 04:01 jasonbahl

@jasonbahl thats fine, its just that I cant figure out how to make it return the url. Its just giving me, "teaserPhotoWide": "312312". Seems to be the ID.

flyingcodeeurope avatar Jan 24 '24 05:01 flyingcodeeurope

@flyingcodeeurope is the value an ID of an image?

If so you could get inspiration from how the Image field does things: https://github.com/wp-graphql/wpgraphql-acf/blob/develop/src/FieldType/Image.php

I'm not familiar with the Image Crop acf field type enough to know how to best support it. I think my best advice would be to draw inspiration from other fields we are supporting and implement in a similar way.

If you have a clear idea of how you would like it to show in the Schema but aren't quite sure how to get it there let me know

jasonbahl avatar Jan 24 '24 14:01 jasonbahl

@jasonbahl I know I want it to output it as url only. Not 100% sure how to achieve that... I played around with the code and actually managed what I wanted. Thank you for your great help pointing me in the right direction.

flyingcodeeurope avatar Jan 24 '24 15:01 flyingcodeeurope

@jasonbahl I know I want it to output it as url only. Not 100% sure how to achieve that... I played around with the code and actually managed what I wanted. Thank you for your great help pointing me in the right direction.

Can you show how you did it? I'm struggling, trying to get the entire image object though...

robkrause avatar Feb 12 '24 18:02 robkrause

@robkrause this is my full code to add it to graphql:

`<?php namespace WPGraphQL\Acf\FieldType;

use GraphQL\Type\Definition\ResolveInfo; use WPGraphQL\Acf\AcfGraphQLFieldType; use WPGraphQL\Acf\FieldConfig; use WPGraphQL\AppContext; use WPGraphQL\Data\Connection\PostObjectConnectionResolver;

add_action( 'wpgraphql/acf/registry_init', function() { register_graphql_acf_field_type( 'image_aspect_ratio_crop', [ 'exclude_admin_fields' => [ 'graphql_non_null' ], 'graphql_type' => static function ( FieldConfig $field_config, AcfGraphQLFieldType $acf_field_type ) { if ( empty( $field_config->get_graphql_field_group_type_name() ) || empty( $field_config->get_graphql_field_name() ) ) { return null; }

				$type_name = $field_config->get_graphql_field_group_type_name();
				$to_type   = 'MediaItem';

				$field_config->register_graphql_connections(
					[
						'description'           => $field_config->get_field_description(),
						'acf_field'             => $field_config->get_acf_field(),
						'acf_field_group'       => $field_config->get_acf_field_group(),
						'fromType'              => $type_name,
						'toType'                => $to_type,
						'fromFieldName'         => $field_config->get_graphql_field_name(),
						'oneToOne'              => true,
						'resolve'               => static function ( $root, $args, AppContext $context, ResolveInfo $info ) use ( $field_config ) {
							$value = $field_config->resolve_field( $root, $args, $context, $info );

							if ( is_object( $value ) && isset( $value->ID ) ) {
								$value = $value->ID;
							}

							if ( is_array( $value ) && isset( $value['ID'] ) ) {
								$value = $value['ID'];
							}

							if ( empty( $value ) || ! absint( $value ) ) {
								return null;
							}

							$resolver = new PostObjectConnectionResolver( $root, $args, $context, $info, 'attachment' );
							return $resolver
							->one_to_one()
							->set_query_arg( 'p', absint( $value ) )
							->get_connection();
						},
						'allowFieldUnderscores' => true,
					]
				);

				return 'connection';
			},
		]
	);
 
 

});

?>`

flyingcodeeurope avatar Feb 14 '24 15:02 flyingcodeeurope

@flyingcodeeurope thanks so much! I was already on the right path but was missing the use statements. The namespace one the other hand was not working in my functions.php

robkrause avatar Feb 16 '24 18:02 robkrause