Can you change Gutenberg Block Type with ACF Pro's acf_register_block function?

Viewed 724

I've been searching for a little while on a way to change one block type to another by using ACF's acf_register_block function and am hitting a wall.

I'm wondering if this is possible, as per the ACF documentation, "Any argument from the JavaScript registerBlockType() function may also be used." (Source: ACF's docs, acf_register_block_type under "$settings").

It's my understanding I should be able to tap into Wordpress' native "transforms" option to be able to convert my block type to another, similar, block type. Source: WP Block API - Block Registration

I'm looking to convert two block types with identical fields - FAQs with Styling to/from FAQs without Styling. Below is my code, contained in my functions.php file.

// faq
    acf_register_block(
      array(
        'name'        => 'faq',
        'title'       => __('FAQ Section'),
        'description'   => __('A custom FAQ block.'),
        'render_callback' => 'my_acf_block_render_callback',
        'category'      => 'faqs',
        'icon'        => 'editor-table',
        'keywords'      => array( 'faq', 'faqs' ),
      )
    );

    // faq no style
    acf_register_block(
      array(
        'name'        => 'faq_no_style',
        'title'       => __('FAQ Section No Styles'),
        'description'   => __('A custom FAQ block.'),
        'render_callback' => 'my_acf_block_render_callback',
        'category'      => 'faqs',
        'icon'        => 'editor-table',
        'keywords'      => array( 'faq_no_style', 'faqs' ),
      )
    );

Does anyone have any input as to if this is possible? Is there a way to create blocks via JavaScript, using ACF's fields? I'm using the latest versions of Wordress and ACF.

I have tried the following without luck:

// faq
    acf_register_block(
      array(
        'name'        => 'faq',
        'title'       => __('FAQ Section'),
        'description'   => __('A custom FAQ block.'),
        'render_callback' => 'my_acf_block_render_callback',
        'category'      => 'faqs',
        'icon'        => 'editor-table',
        'keywords'      => array( 'faq', 'faqs' ),
        'transforms' => array(
          'to' => array(
            'transform' => function() {
              print_r("test");
            },
          )
        )
      )
    );
1 Answers

I just checked on ACF PRO 5.8.8 source code and looks like acf_register_block_type does not support transforms. ACF Blocks are very useful, but they are not as flexible yet, unfortunately. But maybe you can add a field where you indicate different types of your block and add different css class to the block template and style accordingly? That's how I usually do that.

Related