I'm trying to create a custom "columns" block for Gutenberg with ACF & Wordpress, so what I want is a easy to manage columns block, where user can input some number representating the amount of wanted columns (for example, 5) and a for will create that columns.
All worked as well, since I've tried to create multiple innerblocks... My code started this way:
acf_register_block_type([
'name' => "theme-columns",
'title' => __("Columns", "mytheme"),
'description' => __("Columns", "mytheme"),
'category' => "theme-blocks",
'icon' => "grid-view",
'render_callback' => [$this->renderers, "columns_renderer"],
'mode' => "preview",
'supports' => [
'align' => true,
'mode' => false,
'jsx' => true
],
'enqueue_assets' => function(){
wp_enqueue_style("bootstrap");
}
]);
And the columns_renderer
function:
public function columns_renderer()
{
$count = get_field("columns-count");
?>
<div class="row row-cols-1 row-cols-md-3 row-cols-xl-5 justify-content-center">
<?php for($i = 0; $i < $count; $i++): ?>
<div class="col">
<InnerBlocks />
</div>
<?php endfor; ?>
</div>
<?php
}
So, as (not) expected, it's not working because Gutenberg doesn't support multiple <InnerBlocks />
per block... Searching on the web, I've found some people talking about doing this like core/column
block does, using some "hacks"... But I can't undestand what to do...
Can someone help me and give me some way to reach what I need?
Thank you!
-- UPDATE --
Tried to creating a "column" block and settings "columns" to only accept newly created "column" block, but still not working...
public function column()
{
$this->register_block([
'name' => "theme-column",
'title' => __("Column", "mytheme"),
'description' => __("Column", "mytheme"),
'category' => "theme-blocks",
'icon' => "columns",
'render_callback' => [$this->renderers, "column_renderer"],
'mode' => "preview",
'supports' => [
'align' => true,
'mode' => false,
'jsx' => true
],
'enqueue_assets' => function(){
wp_enqueue_style("theme-main");
}
]);
}
public function column_renderer()
{
?>
<InnerBlocks />
<?php
}
public function columns()
{
$this->register_block([
'name' => "theme-columns",
'title' => __("Columns", "mytheme"),
'description' => __("Columns", "mytheme"),
'category' => "theme-blocks",
'icon' => "columns",
'render_callback' => [$this->renderers, "columns_renderer"],
'mode' => "preview",
'supports' => [
'align' => true,
'mode' => false,
'jsx' => true
],
'enqueue_assets' => function(){
wp_enqueue_style("theme-main");
}
]);
}
public function columns_renderer()
{
$allowedBlocks = ["acf/theme-column"];
$template = array(
array('acf/biore-column', []),
);
$column_count = get_field("columns-count");
?>
<div class="row py-4">
<?php for($i = 0; $i < $column_count; $i++): ?>
<div class="col">
<InnerBlocks allowedBlocks="<?= esc_attr(wp_json_encode($allowedBlocks)); ?>" template="<?= esc_attr(wp_json_encode($template)); ?>" />
</div>
<?php endfor; ?>
</div>
<?php
}