This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
Adds filter to register_graphql_field to enable reusing logic of existing ACF types #288
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey there!
The problem
So, at Brius Media we've been using wp-graphql and wp-graphql-acf for exciting new projects.
One thing we've stumbled upon is that, when we create new field types inside ACF, we had some trouble getting wp-graphql-acf to recognize them. Turns out that, inside the register_graphql_field function (in class-config), there's a switch statement that checks for certain acf_types and handles the way they're going to be shown in the API and, when we create a new ACF type, there's no way we can "stick" something in there and add our own logic.
Since what we do, in order to reuse ACF's code, is to extend their original fields and add our own logic on top, all we needed was to "trick" the switch statement into thinking that, for instance, our "foo_bar_field" was a "post_object" field. We started editing wp-graphql-acf's source code directly but, as you can imagine, that's not ideal. We also wanted to contribute since maybe this is a problem for other people.
The solution
We added a filter inside register_graphql_field that allows us to change the acf_type before the switch statement, thus allowing us the "reuse" the already existing logic.
This is a super simple implementation, but I'm more than happy to assist in improving it so it can be merged and help other people.
Keep up the good work, guys!