Hi,
I’m trying to automate the block build process so we only have to make a file in the /block/ directory. It loops the files, gets the variables from the first comment en puts it in the acf_register_block_type(). However it gives an error (translated from dutch): WP_Block_Type_Registry::register is’nt called correctly. Bloktypenames must contain a namespace before the name . E.g.my-plugin/my-custom-block.
My code:
functions.php
<?php
/**
* GUTENBERG INITIALISATION
*/
add_action('acf/init', 'my_guten_acf_init');
function my_guten_acf_init() {
// check function exists
if( function_exists('acf_register_block') ) {
// register a testimonial block
$blockdir = get_theme_file_path('/template-parts/block/');
if(is_dir($blockdir)) {
$dir = new DirectoryIterator($blockdir);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$filedoc = getFileDocBlock($fileinfo->getPathname());
$pattern = "#(@[a-zA-Z]+\s*[a-zA-Z0-9, ()_].*)#";
$arr=array('name'=>'test');
preg_match_all($pattern, $filedoc, $matches, PREG_PATTERN_ORDER);
foreach($matches[0] as $value) {
$output=array();
$value = substr($value,1);
parse_str($value, $output);
$arr[key($output)] = $output[key($output)];
}
acf_register_block_type(array(
'name' => ''.strval($arr['name']).'',
'title' => $arr['title'],
'description' => $arr['description'],
'render_callback' => 'my_guten_acf_block_render_callback',
//'render_template' => 'template-parts/blocks/testimonial/testimonial.php',
'category' => 'my-blokken',
'icon' => 'admin-comments',
'keywords' => explode(',',$arr['tags']),
'supports' => array(
'align' => false,
)
));
}
}
}
}
}
function my_guten_acf_block_render_callback( $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") );
}
}
function my_guten_plugin_block_categories( $categories, $post ) {
//to show other blocks, uncomment $categories
return array_merge(
//$categories,
array(
array(
'slug' => 'my-blokken',
'title' => __( 'My custom blokken', 'my-plugin' ),
'icon' => 'wordpress',
),
)
);
}
add_filter( 'block_categories', 'my_guten_plugin_block_categories', 10, 2 );
function getFileDocBlock($file){
$docComments = array_filter(
token_get_all( file_get_contents( $file ) ), function($entry) {
return $entry[0] == T_DOC_COMMENT;
}
);
$fileDocComment = array_shift( $docComments );
return $fileDocComment[1];
}
in /block/content-{name}.php i have the following comment:
<?php
/**
* Block Name: Header voorpagina
* @name=headervoorpagina
* @title=Header voorpagina
* @tags=header,voorpagina
* @description=de beschrijving
*/ ?>
Is it even possible to get the blocks registered trough a loop?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.