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']
)
);
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 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.