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

Add ACF Fields to the WPGraphQL Schema when registering fields in PHP #188

Closed
jhuon opened this issue Dec 3, 2020 · 3 comments
Closed
Labels
Location Rules Issues related to ACF Location Rules

Comments

@jhuon
Copy link

jhuon commented Dec 3, 2020

I'm registering ACF fields exclusively with PHP (via ACF Builder) and I'm wondering how can I add my fields to WPGraphQL Schema, without using the ACF GUI. I noticed there is an entry on this subject, in the README.md, marked as to do. Is it currently supported (only the documentation has to be written), or is it something that still need to be implemented?

@ChristopherNowlan
Copy link

ChristopherNowlan commented Jan 13, 2021

I wrote this method so I could use ACF Builder

function add_to_graphql( array $fields, string $name ) : array {
	$fields[ 'show_in_graphql' ]    = 1;
	$fields[ 'graphql_field_name' ] = $name;

	return $fields;
}

Than use it like this

function register_option_fields(): void {
       option_fields= [
          'contact',
          'google
       ];
	foreach ( $option_fields as $field ) {
		$fields_path = get_stylesheet_directory() . '/includes/fields/options/' . $field . '.php' ;

		$fields = new FieldsBuilder( $field );
		$option = $fields->setLocation( 'options_page', '==', 'acf-options' )->addFields( include $fields_path );

		acf_add_local_field_group( add_to_graphql( $option->build(), $field ) );
	}
}
add_action( 'acf/init', 'register_options_fields' );

@jasonbahl jasonbahl added this to the ACF Schema Location Rules milestone Mar 5, 2021
@jasonbahl
Copy link
Contributor

👋 Hello,

We've been talking about revamping the way ACF Field Groups are shown in the Schema.

At the moment, the ACF location rules are used to map field groups to the WPGraphQL Schema, but this is causing some funky issues.

So, today, you can register fields in code, use the regular ACF location rules, and this plugin attempts to figure out from the location rules how to add the Field group to the Schema.

In the near-future, you will be able to explicitly select which GraphQL type(s) the ACF Field group should be associated with in the Schema.

I've put this issue in the "ACF Schema Locations" milestone to follow up with when that's resolved.

@jasonbahl jasonbahl added the Location Rules Issues related to ACF Location Rules label Mar 5, 2021
@jasonbahl
Copy link
Contributor

This is now resolved with v0.5.0 (#250)

If you register an ACF Field Group you can explicitly register what GraphQL Types the field group should be associated with using the graphql_types field in the Field Group config array.

You can see this in action in this test, which adds a field group to the Post type and the acfCpt type (which is a Custom Post Type, added to the Schema for testing).

You can also see how Options Pages can be added to the Schema in this test here: https://github.com/wp-graphql/wp-graphql-acf/pull/250/files#diff-6d03e0950b6efd5d3ce4d3014252facca56305c1cbca8bf9f3a0b38d4bdba17fR591-R672

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Location Rules Issues related to ACF Location Rules
Projects
None yet
Development

No branches or pull requests

3 participants