AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
835 stars 171 forks source link

Registering Gutenberg block with $args instead of block.json #815

Open jl-a opened 1 year ago

jl-a commented 1 year ago

Issue

When running register_block_type with a PHP array of arguments instead of a path to block.json ( i.e. using the $args method, as described by the documentation), the ACF Gutenberg block is not visible on the frontend.

(This is an issue for ACF Pro)

Steps to reproduce

With a fresh WordPress install, no plugins activated except ACF Pro, and a blank theme, if functions.php contains the following:

<?php
add_action( 'init', 'register_blocks' );
function register_blocks() {
    register_block_type("acf/test", [
        "title" => "Test ACF block",
        "description" => "A custom block.",
        "category" => "layout",
        "icon" => "editor-alignleft",
        "acf" => [
            "mode" => "auto",
            "renderTemplate" => __DIR__ . "/render.php",
        ],
        "align" => "full"
    ]);
)

the block is not available in the Gutenberg editor.

However if functions.php is changed to

<?php
add_action( 'init', 'register_blocks' );
function register_blocks() {
    register_block_type( __DIR__ . '/block.json' );
}

and a block.json file is created like so:

{
    "name": "acf/test",
    "title": "Test ACF block",
    "description": "A custom block.",
    "category": "layout",
    "icon": "editor-alignleft",
    "acf": {
        "mode": "auto",
        "renderTemplate": "render.php"
    },
    "align": "full"
}

the block is registered and is visible in the Gutenberg editor. This shows that registering the block with the PHP $args method does not work.

Expected behaviour

Registering through register_block_type with the PHP $args method should cause ACF blocks to be usable in the Gutenberg editor, the same as if the block.json method was used.

I filed a support ticket and the response indicated that ACF is supposed to support the $args method so I believe this is a bug.

Versions used

CreativeDive commented 1 year ago

I think, registering a block by register_block_type() with the block_type name instead of the block.json file fails, because it requires an additional block javascript file including edit() and save() functions.

Therefore using the block.json path is recommended.