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

Acf fields return null with repeater/group #170

Closed
lnikell opened this issue Sep 11, 2020 · 17 comments
Closed

Acf fields return null with repeater/group #170

lnikell opened this issue Sep 11, 2020 · 17 comments
Labels
bug Something isn't working close candidate Field Type: Repeater Location Rules Issues related to ACF Location Rules status: awaiting author response Additional information has been requested from the author

Comments

@lnikell
Copy link

lnikell commented Sep 11, 2020

I have the following problem, that I think relates to this plugin:

  • I have a custom post type
  • There are 2 different ACF groups with repeater assigned to 2 different posts within this post type
  • This repeater field named "items"
  • First ACF groups has items with fields "title", "date", second one has items with fields "title", "description", "url"
  • When you query over WP GraphiQL for the second post description and url will be always "null", until you change the name of repeated to a different one. From "items" to "items_second" for example

ACF version - 5.8.12
WP GraphQL - 0.12.3
WPGraphQL for Advanced Custom Fields - 0.3.5 (Tested on 0.4 too)
Custom Post Type UI - 1.8.0

@alexfromesper
Copy link

alexfromesper commented Sep 15, 2020

Yeah this not only occurs with the "Repeater" field type but also the "Group" and "Gallery" field types.
Any types that have children seem to be effected.

It gets quite annoying when you have to rename each field that has child nodes.
This murders scalability when trying to implement scalable structures (I.E. repeating rows, columns, etc)

@rburgst
Copy link

rburgst commented Oct 5, 2020

Note that this was working fine in

  • WP GraphQL: 0.9.1
  • WP GraphQL ACF: 0.3.3
  • ACF : 5.8.7 (note that I can replicate the problem with this version and the latest verison of WP GraphQL+ACF, so IMHO the ACF version is not relevant).

@rburgst
Copy link

rburgst commented Oct 7, 2020

Correction, I am no longer to replicate the issue. My problem was that my DB was broken and the ACFs did indeed have no content any longer. After restoring the DB I can see values of repeater fields even if multiple sub-fields have the same name.

@rburgst
Copy link

rburgst commented Oct 14, 2020

ok, now I can replicate it again with wp-graphql-acf 0.3.5.

I have 2 custom fields which have the same named repeater, lets say they are called acf1 and acf2.

Both of them have a repeater field called link that have (among others) a text fields with the name buttonText.

On the page itself only acf2 is configured.

If I only query acf2 then everything works as expected:

query {
  page(id: "8796053", idType: DATABASE_ID) {
    id
    slug
    title
    acf2 {      
      links {
        buttonText
      }
    }
  }
}

=>

  "data": {
    "page": {
      "id": "cG9zdDo4Nzk2MDUz",
      "slug": "my-page",
      "title": "my page",
      "acf2": {
        "links": [
          {
            "buttonText": "my button text"
          },
          ...

however if I query both fields (even though on the page acf1 is not configured) the data shows up for acf1, rather than acf2.

query {
  page(id: "8796053", idType: DATABASE_ID) {
    id
    slug
    title
    acf1 {
      links {
        buttonText
      }
    }
    acf2 {      
      links {
        buttonText
      }
    }
  }
}

=>

  "data": {
    "page": {
      "id": "cG9zdDo4Nzk2MDUz",
      "slug": "my-page",
      "title": "my page",
      "acf1": {
        "links": [
          {
            "buttonText": "my button text"
          },
          ...
      "acf2": {
        "links": [
          {
            "buttonText": null
          },
          {
            "buttonText": null
          },

@rburgst
Copy link

rburgst commented Oct 30, 2020

I think I tracked down the problem to be an ACF bug, when you query acf1\links via

			$field_value = get_field( $key, $id, $format );

and the $key is the wrong (ie. acf1) field, the actual lookup in the DB will be performed using the field_name i.e. links rather than the field id, then ACF will cache the value in its internal cache using the wrong field ids (since it looked up the values not via id but the field name which is not unique)

So this is most likely an ACF caching bug that does not have a lot to do with wp-graphql-acf (or rather where wp-graphql-acf cannot really work around this problem).

@rburgst
Copy link

rburgst commented Oct 30, 2020

I managed to work around the caching problem by patching acf-value-functions.php in principle you need to search the whole file for $post_id:$field_name and instead use the $post_id:$field_name:$field_key. Need to check the latest ACF pro version whether this has been fixed in more recent versions.

rburgst added a commit to rburgst/acf that referenced this issue Nov 2, 2020
- this avoids having groups with the same field names from clashing
- in the case where you query via `wp-graphql-acf` and 
  you have multiple field groups with repeaters that resolve to the 
  same field name, then without this patch the first field group
  will load the field value and store it inside the cache
  with the wrong sub-field ids.
- for more info see 
   wp-graphql/wp-graphql-acf#170
rburgst added a commit to rburgst/acf that referenced this issue Nov 2, 2020
- this avoids having groups with the same field names from clashing
- in the case where you query via `wp-graphql-acf` and 
  you have multiple field groups with repeaters that resolve to the 
  same field name, then without this patch the first field group
  will load the field value and store it inside the cache
  with the wrong sub-field ids.
- for more info see 
   wp-graphql/wp-graphql-acf#170
@rburgst
Copy link

rburgst commented Nov 2, 2020

created a patch for ACF, lets see if they take it

@lnikell lnikell changed the title Acf fields return null with repeater Acf fields return null with repeater/group Nov 16, 2020
@lnikell
Copy link
Author

lnikell commented Nov 16, 2020

@rburgst I guess it's a bit different issue you're talking about. In my case, if there are multiple objects in DB with the same ACF groups/repeaters names there is some overlapping problem when it just does not show fields.

So for the time being the workaround just name those groups differently.

@rburgst
Copy link

rburgst commented Nov 16, 2020

I believe your problem is exactly the same as mine. As stated above, the problem is wrong caching, the cache is stored with the wrong field id. In my case I also get all null values from graphql.

@rburgst
Copy link

rburgst commented Nov 16, 2020

you can try out my patch (AdvancedCustomFields/acf#403) and see if it helps.

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

@rburgst @lnikell we've been discussing changing how field groups are mapped to the GraphQL Schema, and I hope the changes to that will resolve this.

Right now this plugin tries to interpret the ACF location rules and attempt to map to the WPGraphQL Schema, but there are a lot of things that don't map really well, and issues like this come up.

I believe that if we can better validate what field groups are assigned to which Types in the GraphQL Schema, this should resolve this issue.

I've added this issue to the "ACF Schema Location Rules" milestone so we can keep it top of mind as we work on that problem.

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

lnikell commented Mar 6, 2021

@jasonbahl Thanks for the update. Looking forward to try new version, one it’s released.

@jasonbahl jasonbahl added bug Something isn't working Field Type: Repeater labels Mar 30, 2021
@jasonbahl
Copy link
Contributor

@lnikell @rburgst Hey! I'm preparing v0.5.0 for release (#250).

It has better support for mapping Field Groups to specific Types in the Schema and an option for you to explicitly set which GraphQL Types the Field Group should show on.

If you could test with that release, that would be sweet!

Thanks!

@jasonbahl jasonbahl added the status: awaiting author response Additional information has been requested from the author label Apr 20, 2021
@rburgst
Copy link

rburgst commented Apr 23, 2021

@jasonbahl I rechecked with 0.5.0 and my problem persists, so my patch in AdvancedCustomFields/acf#403 is still required

@jasonbahl
Copy link
Contributor

@rburgst @lnikell would you agree that with @rburgst findings I can safely close this issue as it appears to be an ACF Core issue and not a "WPGraphQL for ACF" issue?

@jasonbahl
Copy link
Contributor

Closing this as there's been no response since May 2021

@jasonbahl
Copy link
Contributor

I would also recommend checking out the new version of the plugin being built over here: https://github.com/wp-graphql/wpgraphql-acf. It's the version that will be released to WordPress.org and then this repo will be archived.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working close candidate Field Type: Repeater Location Rules Issues related to ACF Location Rules status: awaiting author response Additional information has been requested from the author
Projects
None yet
Development

No branches or pull requests

4 participants