AdvancedCustomFields / AdvancedCustomFields/acf
ACF get_fields function does not work with copied ACF blocks in gutenberg
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 945
- Forks
- 197
- PR merge metrics
- No merged PRs in 30d
Description
We are you using a lot ACF blocks in gutenberg and we found an issue with it in the WP JSON API.
When you are creating ACF blocks and copy them to other pages the blockID is the same.
Then we have a function for the register_rest_field to get the blocks in the REST API so we can sync them to our frontend.
We do so like this:
$blocks = parse_blocks( $post->post_content );
FOR EACH BLOCKS
acf_setup_meta( $block['attrs']['data'], $block['attrs']['id'], true );
$fields = get_fields();
acf_reset_meta( $block['attrs']['id'] );
When you ask for a batch pages in the REST API like this:
https://domain.com/wp-json/wp/v2/pages/?page=9&per_page=3
The block field for all the pages on the URL will have the exact same ACF values as the first page.
The bug is in acf-value-functions.php
- function acf_get_value (rule 91-92)
- function acf_format_value (rule 143-144)
Here you they are posting values to the store with this code:
// Update store.
$store->set( "$post_id:$field_name:formatted", $value );
The problem is the $post_id for ACF blocks is the blockId which is not unique if you copy ACF blocks from page to page.
The solution would be to make the store something like this:
// Update store.
$store->set( "$post_id:$block_id:$field_name:formatted", $value );
This way you have the real post_id and the block_id and the store key will be unique.
When I comment the store set method in both functions everything works as aspected.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in acf-value-functions.php at acf_get_value and acf_format_value, then reproduce the issue by requesting a batch of pages through the REST API with copied ACF blocks. Verify that each page's block fields retain their own values and that the store keys distinguish the real post and block identities.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php, wordpress
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100