AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
877 stars 182 forks source link

Duplicating Gutenberg blocks in edit mode erases data upon saving the post #923

Open mrdarrengriffin opened 4 months ago

mrdarrengriffin commented 4 months ago

Describe the bug When duplicating a block with ACF fields in Gutenberg, there is an issue where if there are 2 or more of the same block on the page, the newly added block will not save correctly. Gutenberg does not correctly display the fact that the fields are empty which is causes a massive friction point for content loading.

To Reproduce Steps to reproduce the behavior. I did this on a fresh install of WordPress 6.1.1 via Local. I installed ACF Pro 6.3.4 with no other plugins active. I was using the Twenty Twenty Four theme.

  1. Open the functions.php file from the active theme
  2. Copy the example provided here into the bottom of the file: https://www.advancedcustomfields.com/blog/acf-5-8-introducing-acf-blocks-for-gutenberg/#functionsphp
  3. In the WordPress admin area, go to ACF > Field Groups and create a block. For my example, I created a "Test Block" group with a "Title" text field. I ensured that the newly created block was chosen in the field block settings rules.
  4. Create a new page
  5. Insert the "Testimonial" block, enter edit mode and type something in the "Title" ACF field.
  6. Publish the page and once saved, refresh the page. You will notice the field shows the correct value
  7. Duplicate the testimonial block using the toolbar menu or CTRL+SHIFT+D
  8. Make an alteration to the field in the newly duplicated testimonials block
  9. Save the page and then refresh. You will first be asked if you want to leave and discard changes. Once the page loads, you will notice both fields show the correct values
  10. Duplicate the bottom testimonial block and change the title once again to something different
  11. Save the page and then refresh. You will notice that only the first two blocks have the correct value. The block we just made has a blank field.
  12. Make a change to the now empty field
  13. Save the page and then refresh. You will first be asked if you want to leave and discard changes. Once the page loads, you will notice that the last block has the correct field value.

Please note:

Expected behavior When duplicating blocks, the data associated with the original block should be retained. Upon saving and refreshing, the data should persist.

Screenshots or Video https://github.com/user-attachments/assets/e12d4494-e54a-458e-aaae-dd895f92cc17

Code Test field group: acf-export-2024-07-29.json

Example code used for testimonial block:

add_action('acf/init', 'my_acf_init');
function my_acf_init() {

    // check function exists
    if( function_exists('acf_register_block') ) {

        // register a testimonial block
        acf_register_block(array(
            'name'              => 'testimonial',
            'title'             => __('Testimonial'),
            'description'       => __('A custom testimonial block.'),
            'render_callback'   => 'my_acf_block_render_callback',
            'category'          => 'formatting',
            'icon'              => 'admin-comments',
            'keywords'          => array( 'testimonial', 'quote' ),
        ));
    }
}

Version Information:

Additional context Add any other context about the problem here.

mattgrshaw commented 4 months ago

Hi @mrdarrengriffin,

Thanks for the detailed report! We've been able to replicate this on our end and will look at fixing this up in a future release.

mrdarrengriffin commented 4 months ago

Hi @mattgrshaw, out of curiosity, do you know what sort of timeline we're looking at? I just need to advice our team and content loaders as to when we can expect a fix. Thanks!

maksymmamontov commented 3 months ago

hi @mattgrshaw could you please update on the timelines for this? we're struggling with this issue on all projects with ACF right now.

block duplication is basically not working at all at ACF Version 6.3.5.

kmwalsh commented 3 months ago

@maksymmamontov It's not at all a real workaround (especially for clients). Switch to block editor Code mode, copy the block markup manually, and paste it in in code mode, then switch back to editing mode to modify things as needed. That seemed to have worked on the site I tried it on.

maksymmamontov commented 3 months ago

@kmwalsh yes, this helps and as developers we usually do it this way but clients are sometimes confused why it's not working, just as with this bug (but it's not ACF-related): https://github.com/WordPress/gutenberg/issues/38731

selceeus commented 3 months ago

I have run across this same issue or a similar one this week. I would add that copy/pasting blocks have the same results. I had to roll back the ACF Plugin to version 6.2.10. Versions 6.3 and up had the issue. Upon upgrade to 6.3 and up any duplicated block reverted to having these issues and would not render to the front end.

cbirdsong commented 3 months ago

I believe I've also run into this issue and have had to roll back. This bug has caused major issues with data loss for a client of ours. It definitely merits a quick point release fix if possible.

Does anyone know if disabling validation sidesteps the issue? If so, it would be ideal if there was a way to do that in blocks registered using acf_register_block_type, or even globally using a filter.

cbirdsong commented 3 months ago

Per support, validation is always disabled on blocks registered using acf_register_block_type, so that doesn't help. Just have to revert and wait for a fix.

darren-strategiq commented 3 months ago

@mattgrshaw, please can we get an update on this issue and when it will likely to be fixed? It's clear that many people are affected by this and is causing issues for their clients.

Thanks in advance

mattgrshaw commented 3 months ago

Hey folks, we're planning to fix this up as part of our next major release. We don't have an ETA on that just yet, but it is in development and we'll update this thread as we have more to share.

cbirdsong commented 1 month ago

I see the first alpha of 6.4 has been released, but it only discusses Woocommerce HPOS. Will this fix be pulled into a later alpha?

Inclushe commented 1 month ago

This issue had been haunting one of our clients for weeks. Downgrading to 6.2.10 as @selceeus suggested fixed the issue for us. @mattgrshaw Is the fix for this planned for 6.4?

maksymmamontov commented 1 month ago

Is there any timeline on fixing this issue?