Register an ACF block with modern practices

Is it possible to register an ACF block using block.json?

I’ve looked at this old post, but that still registers the block using acf_register_block_type

In my block.json:

"acf": {
		"mode": "preview",
		"renderTemplate": "/resources/views/blocks/dummy-box/dummy-box.php"

	}

ACF fails to see my template however.

BTW, I used this handy utility to create the block setup: https://github.com/thetwopct/create-acf-block-json

After registering it with the correct path, it seems to have found it, I think.

add_action( 'init', 'register_acf_blocks' );
function register_acf_blocks() {
    register_block_type( __DIR__ . '/resources/views/blocks/dummy-box' );
}

Unfortunately, it doesn’t work with blade files; just strict php.

Would love to understand if there is a way to make this work with the Blade files :crossed_fingers:

It looks like ACF provide a render callback for block.json:

  • renderCallback (string) (optional) Instead of providing a render_template, a callback function name may be specified to output the block’s HTML.
// Specifying a function
"renderCallback": "my_acf_block_render_callback",

You could render a Blade view inside that callback using the view() function.

function your_blade_block_render_callback($block, $content, $is_preview, $post_id, $wp_block, $context) {
    echo view('blocks/example', compact('block', 'content', 'is_preview', 'post_id', 'wp_block', 'context'));
}

I haven’t tested this, but having a quick look at the ACF source shows that this is fired via call_user_func and return value discarded, so echoing rather than returning the output seems to be the way it’s supposed to work.

Slightly adjacent to this is this page from the Acorn docs on rendering Blade views.