Testing: render_block_context POC#7420
Testing: render_block_context POC#7420mukeshpanchal27 wants to merge 4 commits intoWordPress:trunkfrom
render_block_context POC#7420Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The POC code used from #7344 (comment) If we refactor core code per POC the ancestors context will be available in For Column block i add two context. 1) how much column [available_context:protected] => Array
(
[postId] => 6
[postType] => page
[wpp-columns] => 1
[wpp-col-width] => 50%
)If we use /*
* Inspiration taken from current block binding implementation.
* https://github.com/WordPress/wordpress-develop/blob/6.6/src/wp-includes/class-wp-block-bindings-registry.php#L193-L204
*/
add_filter(
'get_block_type_uses_context',
function ( $uses_context, $block_type ) {
if ( 'core/image' === $block_type->name ) {
// Use array_values to reset the array keys.
return array_values( array_unique( array_merge( $uses_context, array( 'wpp-columns','wpp-col-width' ) ) ) );
}
return $uses_context;
},
10,
2
);[context] => Array
(
[postId] => 6
[postType] => page
[wpp-columns] => 1
[wpp-col-width] => 50%
) |
felixarntz
left a comment
There was a problem hiding this comment.
@mukeshpanchal27 A few questions on this. Most importantly, I don't understand how the parent block is set. Not related to this PR, even just generally in Core today.
| * } | ||
| * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. | ||
| */ | ||
| $context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block ); |
There was a problem hiding this comment.
Question: How is $parent_block ever set here? Looking at the full function, it seems that it is only set to null at the top of the function, but then never changed. Will this always be null? 🤔
There was a problem hiding this comment.
It was introduce in https://core.trac.wordpress.org/changeset/51894 with message.
Introdices another param to these filters: $parent_block that is the "parent" WP_Block instance for nested blocks and null for top level blocks. Adds unit tests for the filters.
There was a problem hiding this comment.
Yes, but my question was how does this actually work? As far as I can tell $parent_block is always null. Does it ever get populated with an actual WP_Block instance anywhere?
There was a problem hiding this comment.
I don't think. It always null for parent block.
There was a problem hiding this comment.
But then is that a bug? How does the parent block ever get set for any block?
| } | ||
|
|
||
| $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry ); | ||
| $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry, $parent_block ); |
There was a problem hiding this comment.
Is this right? Shouldn't the parent block of the inner blocks of this block be $this? Passing $parent_block here I think would effectively pass the "grandparent block".
There was a problem hiding this comment.
No effect if i pass $this
There was a problem hiding this comment.
Can you please clarify what you mean? How do you mean there's no effect?
Logically, the child blocks should access their parent block. But by passing the parent block of the current block here to its child block(s), you're effectively passing the grandparent block, which seems wrong.
| $this->block_type = $registry->get_registered( $this->name ); | ||
|
|
||
| $this->available_context = $available_context; | ||
| $this->available_context = apply_filters( 'render_block_context', $available_context, $block, $parent_block ); |
There was a problem hiding this comment.
I know this is a POC, but just noting that if we proceed with implementing this, this will need the original filter comment doc to be added.
|
Closing in favour of #7522 |
Testing PR. DO NOT REVIEW.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.