Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACF Location screen context now has "wp-graphql-acf" set to true, thi… #134

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

AbeCole
Copy link

@AbeCole AbeCole commented May 5, 2020

This allows for location rule validation for Field Groups to be overridden, allowing certain location rules that are not relevant to GraphQL to be ignored.

For example currently 'page template' rules will never return 'true' (thus hiding any Field Groups that have these rules set from displaying in GraphQL), we can use the ACF filter acf/location/rule_match/$rule_param:

add_filter( 'acf/location/rule_match/page_template', 'ignore_acf_location_rules_for_graphql', 10, 4 );
function ignore_acf_location_rules_for_graphql( $result, $rule, $screen, $field_group ) {
  if (isset($screen['wp-graphql-acf']) && $screen['wp-graphql-acf'] == true) return true;

  return $result;
}

Probably be good to add this example to documentation but maybe someone else has changes to the format I've added.

@jasonbahl
Copy link
Contributor

@AbeCole I've been discussing in Slack with some folks revamping how Field Groups can be assigned to the Schema. I opened this issue to explain further: #135

The plan is to no longer make use of the location rules at all, but set a more explicit relationship between Field Groups and the WPGraphQL Schema.

@moxdev
Copy link

moxdev commented Jan 6, 2021

This allows for location rule validation for Field Groups to be overridden, allowing certain location rules that are not relevant to GraphQL to be ignored.

For example currently 'page template' rules will never return 'true' (thus hiding any Field Groups that have these rules set from displaying in GraphQL), we can use the ACF filter acf/location/rule_match/$rule_param:

add_filter( 'acf/location/rule_match/page_template', 'ignore_acf_location_rules_for_graphql', 10, 4 );
function ignore_acf_location_rules_for_graphql( $result, $rule, $screen, $field_group ) {
  if (isset($screen['wp-graphql-acf']) && $screen['wp-graphql-acf'] == true) return true;

  return $result;
}

Probably be good to add this example to documentation but maybe someone else has changes to the format I've added.

Does this work around still work? I added the code snippet to my theme but still not seeing the graphql fields when using multiple acf location rules. Is there any other way to get around say showing a field on all pages except the home page?

@AbeCole
Copy link
Author

AbeCole commented Jan 6, 2021

@moxdev It still works for me however you need to add additional 'add_filter' hooks depending on which 'location rules' you are using. The example you've quoted will only override the 'page_template' rule.

If you are using multiple, for example lets say you have a field group with a 'page_template' & 'post format' & 'post taxonomy' rules, you need to add multiple hooks like so:

add_filter( 'acf/location/rule_match/page_template', 'ignore_acf_location_rules_for_graphql', 10, 4 );
add_filter( 'acf/location/rule_match/post_format', 'ignore_acf_location_rules_for_graphql', 10, 4 );
add_filter( 'acf/location/rule_match/post_taxonomy', 'ignore_acf_location_rules_for_graphql', 10, 4 );
function ignore_acf_location_rules_for_graphql( $result, $rule, $screen, $field_group ) {
  if (isset($screen['wp-graphql-acf']) && $screen['wp-graphql-acf'] == true) return true;

  return $result;
}

@moxdev
Copy link

moxdev commented Jan 6, 2021

Ive tried everything I can think of cant get "page_template" to work. Doesn't seem to ignore any rules I set "post_format" "post_taxonomy" etc. Show in GraphQL is on for all fields and tried changing the "GraphQL Field Name" thinking maybe it was caching it. Im using the latest version of all the plugins. Any other ideas?

@AbeCole
Copy link
Author

AbeCole commented Jan 6, 2021

Ive tried everything I can think of cant get "page_template" to work. Doesn't seem to ignore any rules I set "post_format" "post_taxonomy" etc. Show in GraphQL is on for all fields and tried changing the "GraphQL Field Name" thinking maybe it was caching it. Im using the latest version of all the plugins. Any other ideas?

The 'page_template' rule I've added as an example, if the field group you are trying to display doesn't use that rule then you don't need to add the filter like I have (instead you hook a different action). If you can provide a screenshot of the rules for the field group you are trying to get then I can tell you which actions would need to be hooked.

I can't understand exactly where your at (if you have source code I can see it will be a lot easier to help). Have you actually made the changes in this commit? I.e. adding 'add_wp_graphql_acf_to_acf_screen', 'get_acf_field_groups' and related changes to

@moxdev
Copy link

moxdev commented Jan 8, 2021

My mistake I missed a line in the commit got it working now thanks for the help!

@rsm0128
Copy link
Contributor

rsm0128 commented Jan 19, 2021

Hi, @moxdev I created a PR for explicit options on the field group edit panel. You can test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants