AdvancedCustomFields / AdvancedCustomFields/acf

Update ACF blocks via block API dosen't work as expected

Open
#817 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

Hey @lgladdy,

lately I've been learning more about the WordPress Block Editor API. There are many useful functions that we can use to interact with blocks. I noticed that using the updateBlockAttributes() function applied to ACF blocks does not work as expected.

wp.data.dispatch('core/block-editor').updateBlockAttributes(clientId, {
    data: { ... },
});

or

wp.data.dispatch('core/block-editor').updateBlock(clientId, {
    attributes: { 
        data: { 
            field_name: 'field_value'
        }
    }
} );

What works is that after updating the ACF block attributes in the above way, the block is updated with the newly set attributes. It seems render_block_preview gets the job done after swapping the block attributes.

But the BlockForm in the sidebar contains the old settings. That means, if I change a field after updating the block in the above way, the updated block reverts to the original state.

The behavior is understandable as the BlockForm was not updated when the Block was updated via the API. Unfortunately, this causes updating the block not to work that way.

Ultimately, only the BlockForm would have to be updated. Is it possible for ACF to update the BlockForm as well?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with WordPress Block Editor API calls to update an ACF block, then inspect how render_block_preview and the sidebar BlockForm handle the block attributes. Done means API updates are reflected in the BlockForm and later field edits no longer revert those changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, wordpress
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.