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

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

Open AbeCole opened this issue 4 years ago • 7 comments

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.

AbeCole avatar May 05 '20 14:05 AbeCole

@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: https://github.com/wp-graphql/wp-graphql-acf/issues/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.

jasonbahl avatar May 05 '20 15:05 jasonbahl

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?

moxdev avatar Jan 06 '21 18:01 moxdev

@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;
}

AbeCole avatar Jan 06 '21 18:01 AbeCole

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?

moxdev avatar Jan 06 '21 21:01 moxdev

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

AbeCole avatar Jan 06 '21 23:01 AbeCole

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

moxdev avatar Jan 08 '21 20:01 moxdev

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

rsm0128 avatar Jan 19 '21 17:01 rsm0128