Creating an ACF Block using a Blade template

Hi,

I’ve been trying to create an ACF Block that renders a Blade template following this discussion as a guide, using the renderCallback approach because I’m aware that acf_register_block_type() is deprecated. However, I’m running into an issue where no parameters are being passed into my callback function. My codebase looks something like this:

resources/views/blocks/new-block/block.json:

...
"acf": {
    "mode": "auto",
    "renderCallback": "\\App\\blade_render_callback",
    "postTypes": ["page"],
    "blockVersion": 2
},
...

resources/views/blocks/new-block/new-block.blade.php:

<p>Hello world</p>

app/controllers/App.php:

public function blade_render_callback($block, string $content = '', bool $is_preview = false, int $post_id = 0, array $wp_block = [], array $context = [])
    {
        echo \Roots\view('blocks/new-block')->render();
    }

This throws an error where the blade_render_callback() expects at least 1 argument to be passed, but receives 0. If I change my controller function to provide a default value of an empty array for $block, I get a different error in vendor/roots/acorn/src/Roots/helpers.php due to a call to an undefined function app() on line 73.

Any advice on how to fix this issue would be greatly appreciated.

Thanks! :slight_smile:

I’d suggest looking into log1x/acf-composer

This seems like it could be incredibly useful, although at the minute I’m using Sage 9 and acf-composer looks like it only works with Sage 10 and above. Any ideas on how I can get my current approach working (I’d like to avoid the time investment in updating to Sage 10 if possible), or is renderCallback not working as expected a known issue?

I’ve mostly used log1x/acf-composer myself so I can’t share any good alternatives at this point.

1 Like