AdvancedCustomFields / AdvancedCustomFields/acf

ACF breaks the block editor if blocks are rendered inside of an ACF block

Open
#837 3 comments 0 reactions 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,

I just wanted to let you know that the block editor hangs as soon as you render block content within an ACF block. I suspect this only occurs when inner blocks are present.

I know this bug has been fixed in the past: https://github.com/AdvancedCustomFields/acf/issues/682. But now it doesn't seem to be working anymore.

This error only occurs if the block content has not yet been cached by ACF. That's why I haven't noticed it for a long time.

I think you should know that.

I have now fixed this bug for myself by doing some operations in my ACF blocks render callback function, but I think this bug should also be fixed directly at ACF:

In my callback function:

function my_acf_block_render_callback( $block, $content, $is_preview, $post_id, $wp_block, $context ) {

    //...
    
    /*
    * HOTFIX: ACF breaks the editor if block content is rendered via do_blocks();
    */

    if( $is_preview && doing_filter('render_block_content_in_editor_filter') ) {
        $block_content = acf_render_blocks_in_editor( $block_content, $content, $wp_block, $block );
        echo $block_content;
        return;
    }
    
    //...
    
}

To convert <InnerBlocks> tags to content:

function acf_render_blocks_in_editor( $html, $content, $wp_block, $block ) {

    $content = str_replace( '$', '\$', $content );

    // Wrap content in our acf-inner-container wrapper if necessary.
    if( $wp_block && apply_filters( 'acf/blocks/wrap_frontend_innerblocks', true, $block['name'] ) ) {

        // Check for a class (or className) provided in the template to become the InnerBlocks wrapper class.
        $matches = array();

        $class = 'acf-innerblocks-container';
        if( preg_match( '/<InnerBlocks(?:[^<]+?)(?:class|className)=(?:["\']\W+\s*(?:\w+)\()?["\']([^\'"]+)[\'"]/', $html, $matches ) ) {
            $class = isset( $matches[1] ) ? $matches[1] : 'acf-innerblocks-container';
        }

        $content = '<div class="' . $class . '">' . $content . '</div>';
    }

    return preg_replace( '/<InnerBlocks([\S\s]*?)\/>/', $content, $html );

}

To output rendered block content in the editor:

function get_block_content( $post_id ) {

    $post = get_post( $post_id );
    $content = isset( $post->post_content ) ? $post->post_content : '';

    return do_blocks( $content );

}

add_filter( 'render_block_content_in_editor_filter', 'get_block_content' );

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 hang with an ACF block containing InnerBlocks and uncached content. Read the render callback and the named acf_render_blocks_in_editor(), get_block_content(), and render_block_content_in_editor_filter paths in the issue. Done means the block editor no longer hangs when nested block content is rendered before ACF caches it.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.