Issue
I have a setup using Gutenberg blocks via ACF. One of these blocks is a Group block which has the following context:
"providesContext": {
"acf/groupData": "data"
}
I am then using <InnerBlocks />
inside the group block template file.
Each of the blocks that can be nested in this group block have a value of:
"usesContext": ["acf/groupData"]
This works perfect and the group values are passed into the $context
array for use within the inner blocks.
However, if I add a Synced Pattern block inside this group block, the context is lost and acf/groupData
is not part of the $context
array.
Hoping someone can help, would be great if there's a way to pass down the group context into synced patterns.
Tried
I've had a look around to see if there's any hooks relating to when patterns are created to see if I can pass the context through there but no luck.
I also tried creating a function that would grab the post content, parse the blocks and try and get the group block from there, however, the block ID's aren't returned when using the post content, only the block name and attributes etc.
Issue
I have a setup using Gutenberg blocks via ACF. One of these blocks is a Group block which has the following context:
"providesContext": {
"acf/groupData": "data"
}
I am then using <InnerBlocks />
inside the group block template file.
Each of the blocks that can be nested in this group block have a value of:
"usesContext": ["acf/groupData"]
This works perfect and the group values are passed into the $context
array for use within the inner blocks.
However, if I add a Synced Pattern block inside this group block, the context is lost and acf/groupData
is not part of the $context
array.
Hoping someone can help, would be great if there's a way to pass down the group context into synced patterns.
Tried
I've had a look around to see if there's any hooks relating to when patterns are created to see if I can pass the context through there but no luck.
I also tried creating a function that would grab the post content, parse the blocks and try and get the group block from there, however, the block ID's aren't returned when using the post content, only the block name and attributes etc.
Share Improve this question asked Nov 18, 2024 at 20:02 Glen SatchwellGlen Satchwell 111 silver badge2 bronze badges 8 | Show 3 more comments3 Answers
Reset to default 1I figured it out! I'm posting here for any future developers.
The other answers to this question weren't possible since the synced patterns being created aren't being registered through the code and instead in the Admin Editor using the options menu for blocks.
What I ended up doing was:
- Hook into the
render_block_data
filter. Check if the block type is core/block (Pattern) and has a parent block. If so, set the 'content' attribute onto the block in question. The pattern block renderer will treat this as overriding data which is then supplied to the block context. For some reason the$context
array within the block template won't pass through the pattern/overrides straight away, which is where the next step helps. - Hook into the
render_block_context
filter. Check if the$context
array contains 'pattern/overrides'. If so, set the acf/groupData key (What we usually have available for non-pattern blocks) equal to the pattern/overrides data. This makes the data available to use within the block template发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745597169a4635186.html
<InnerBlocks>
have anallowedBlocks
attribute? And if so, are all of the pattern blocks on that list? Have you tried dumping the block data before save or while editing? You might find help on the plugin support forums: wordpress./support/plugin/advanced-custom-fields – admcfajn Commented Nov 18, 2024 at 21:16