Gutenberg blocks can have parent blocks, which makes them only available within blocks that are designated as a parent. However, ACF blocks don’t seem to have that. Am I missing something? If not, will that be added?
My use case:
I have block that can have blocks added to it. One of the blocks will be a post selector block (ACF block) or a taxonomy selector (ACF block). So, I want the ACF blocks to be available to add within the parent block, but not be available to add on its own.
Ref:
Following up on this… Still looking to see if there is a way to set a parent for blocks registered with ACF?
In case anyone else is looking for an answer, I found a solution…
Use block filters.
The blocks.registerBlockType
filter lets you modify block settings. So, I just added a parent setting with the block I wanted as a parent. Since the parent prop takes an array of blocks, more than one parent can be named.
/**
* Set ACF block parent
* @param {obj} settings
* @param {string} name
*/
function setBlockParent( settings, name ) {
if ( name !== 'acf/post-content' ) {
return settings;
}
return lodash.assign({}, settings, {
parent: [
'namespace/featured-content',
]
} );
}
addFilter(
'blocks.registerBlockType',
'namespace/set-post-content-parent',
setBlockParent
);
Hope it helps anyone else.
ref: https://developer.wordpress.org/block-editor/developers/filters/block-filters/#filters
If somebody lands here as I did: ACF silently supports “parent” attribute in acf_register_block function:
\acf_register_block(
array(
// your attributes
'parent' => ['acf/parent-block']
)
);
https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/#parent-optional
sure. you can also add
'color' => array(
'link' => true
)