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
}
I've actually been doing something very similar, although I am stuck on a different issue now.
It looks like you are still trying to create multiple
InnerBlocksin yourcolumns_rendererfunction. Instead, what I do is I create oneInnerBlocksand fill it with a$templatemade up of multipleColumnblocks.I use
templateLock="all"on theRowInnerBlocksto stop any new blocks being added there, something similar could also be achieved by usingallowedBlocksdepending on if you want to be able to add moreColumnsfrom the visual editor or not. I addtemplateLock="false"to theColumnsInnerBlocksin order to overwrite the parent (Row) value and allow content to be added into it.I use
acfto create acolsfield with a numeric range slider for theRowblock, default value is1to create 1 column. As you move the slider, moreColumnswill be added.Setting up the blocks:
Outputting the
Rowcontent:Outputting the
Columnblock: