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']
)
);
The topic ‘Register Block Parent Attribute’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.