New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try: Custom block insertion hooks #45098
base: try/example-block-hooks
Are you sure you want to change the base?
Conversation
Hi @ockham, @woocommerce/mothra Apart from reviewing the code changes, please make sure to review the testing instructions as well. You can follow this guide to find out what good testing instructions should look like: |
// Parse these first so markup is generated from all inner blocks. | ||
$first_chunk = $this->insert_blocks( $parsed_inserted_block, 'first_child', $hooked_blocks, $context ); | ||
$last_chunk = $this->insert_blocks( $parsed_inserted_block, 'last_child', $hooked_blocks, $context ); | ||
array_unshift( $parsed_inserted_block['innerContent'], $first_chunk ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs further investigation to make sure this won't break things. I suspect this could be problematic depending on existing chunks in innerContent
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to prepend/append the hooked block to innerBlocks
rather than innerContent
:
array_unshift( $parsed_inserted_block['innerContent'], $first_chunk ); | |
array_unshift( $parsed_inserted_block['innerBlocks'], $first_chunk ); |
Additionally, you also need to insert a null
in the corresponding position in innerContent
, so it should actually read
array_unshift( $parsed_inserted_block['innerContent'], $first_chunk ); | |
array_unshift( $parsed_inserted_block['innerBlocks'], $first_chunk ); | |
array_unshift( $parsed_inserted_block['innerContent'], null ); |
The way it works is that innerContent
can have items that are either HTML strings, or null
to mark a position where an item from innerBlocks
needs to be interpolated. E.g.
$group_block_with_some_inner_block = array(
'blockName' => 'core/group',
'attrs' => array(),
'innerBlocks' => array( $some_inner_block ),
'innerContent' => array(
'<div class="wp-block-group">',
null,
'</div>'
),
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still not the full picture BTW if you want to account for some subtleties: As the Group block example demonstrates, you might not actually want to put the null
marker for first_child
in the very first position in innerContent
(or the one for last_child
in the very last one), as you might want to account for the wrapping HTML:
$group_block_with_some_inner_block = array(
'blockName' => 'core/group',
'attrs' => array(),
'innerBlocks' => array( $hooked_first_child, $some_inner_block ),
'innerContent' => array(
null, // $hooked_first_child is going to be rendered outside the wrapping div :/
'<div class="wp-block-group">',
null,
'</div>'
),
);
Instead, you'll likely want to find the first (last) non-HTML location in innerContent
. For the original Block Hooks implementation (in GB's WP 6.4 compat shim), we ended up with the following logic: https://github.com/WordPress/gutenberg/blob/98552b2486b52e4dd3c90775c2a029b95e64253c/lib/compat/wordpress-6.4/block-hooks.php#L157-L182
Test Results SummaryCommit SHA: 73fce83
Please address the following issues prior to merging this pull request: |
Changes proposed in this Pull Request:
This PR uses a custom callback for the before/after block visitors and introduces custom hooks to allow insertion of blocks. It differs in the current block hooks in that:
How to test the changes in this Pull Request:
Changelog entry
Significance
Type
Message
Comment