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.
2
Answers
Unfortunately, Synced Patterns (previously known as "Reusable Blocks") do not inherit context by design, because of how they are intended to be used across posts/pages and templates so they do not rely on context being provided.
Reviewing your scenario, the Group block provides context of
"acf/groupData"
toInnerBlocks
in the Editor, which works, although block context cannot be saved. Each of your nested blocks withinInnerBlocks
consume"acf/groupData"
and if they are rendered dynamically, they can use the provided context on the frontend, eg:render.php
If you then saved this as a Synced Pattern, a warning
Undefined array key “acf/groupData”
is triggered as the parent blocks’ context does not exist to the Synced Pattern because Synced Patterns are actually saved as a custom post type (wp_block) in the database to be reused/included in other content.Possible Workaround
Instead of Synced Patterns, I would:
InnerBlocks
Template which removes the need for Synced Patterns while maintaining the context between Parent – Child."acf/groupData"
to be rendered on the frontend (see render.php example above).The context loss with Synced Patterns is a known limitation because patterns are treated as separate entities. Here are two potential solutions:
Then modify your group block to pass data as attributes:
The first approach is more reliable but requires pattern modification. The second is global but may have edge cases.