Freddy
Freddy

Reputation: 867

"No block type exists" when trying to create ACF block for Gutenberg

I have a new block that I'm trying to init which is called hero. At the moment, I'm trying to assign an ACF field group to that block, but currently seeing this in the WordPress backend:

enter image description here

I have created the block and registered the block type, but unable to see where things are not working?

Here is my approach:

/acf-blocks/blocks.php

<?php

function register_acf_block_types() {

    $hero = array(
        'name'              => 'hero',
        'title'             => __('Hero'),
        'description'       => __(''),
        'render_callback'   => 'block_render',
        'category'          => 'formatting',
        'icon'              => 'admin-comments',
        'keywords'          => array( 'hero' ),
    );

}


$blocks = [
  $hero
];

return $blocks;


// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') ) {
  add_action('acf/init', 'register_acf_block_types');
}


?>

/acf-blocks/render.php

<?php

/**
 *  This is the callback that renders the block.
 */

 function block_render( $block ) {

    // convert name ("acf/testimonial") into path friendly slug ("testimonial")
    $slug = str_replace('acf/', '', $block['name']);

    // include a template part from within the "template-parts/block" folder
    if( file_exists( get_theme_file_path("/template-parts/block/content-{$slug}.php") ) ) {
        include( get_theme_file_path("/template-parts/block/content-{$slug}.php") );
    }
 }

?>

/acf-blocks/functions.php

<?php


function block_acf_init(){
  $blocks = require(__DIR__.'/inc/acf-blocks/blocks.php');

  foreach($blocks as $block) {
    acf_register_block($block);
  }
}


?>

/template-parts/block/hero.php

<?php

/*
* Block Name: hero
*/


?>

Here is my folder structure:

theme
   inc
      acf-blocks
         blocks.php
         functions.php
         render.php
   template-parts
      blocks
         hero.php

Upvotes: 2

Views: 1679

Answers (1)

Vortac
Vortac

Reputation: 568

Change the ACF function in your foreach loop (in /acf-blocks/functions.php) to:

foreach($blocks as $block) {
    acf_register_block_type($block);
}

Upvotes: 0

Related Questions