Gutenberg: `render_block_data` for nested blocks - WP_Block::render vs render_block

Created on 7 Oct 2020  路  3Comments  路  Source: WordPress/gutenberg

Describe the bug
Since WordPress 5.5 the WP_Block class with it's render method has been added. Now render_block is only called for all top-level blocks which calls WP_Block::render, each nested (aka innerBlocks) blocks are created in WP_Block::render as WP_Block instances and only rendered with this method.
This means that hooks from render_block (pre_render_block, render_block_data, render_block_context) only get called for top-level blocks. This leads to multiple problems:

  1. There's an inconsistency in rendering depending on if a block is used top-level or in another block (eg group). This may lead to bugs.
  2. Also the Latest Posts block uses this hook to parse som data (or migrate it?) - see wp-includes/blocks/latest-posts:208 . This method is not called if the Latest Posts block is in a Group block.
  3. I'm not sure I can give the same information to a block via block-context as with render_block_data before.

My use case:
I'm using the 'render_block_data' method to add some custom information (eg. a helper method which parses the is-style-{style} className from the attributes and sets it as variable on the block data.

I didn't read any documentation about this change, but have to admit I still have to read everything about block contexts.

https://core.trac.wordpress.org/ticket/49926

21467

To reproduce
Steps to reproduce the behavior:

  1. Create a new Post and insert a Group block
  2. Insert a nested Latest Posts Block
  3. block_core_latest_posts_migrate_categories is not called (actually it's called but not for the core/latest-posts block)

This can be reproduced for any nested block and any hook to render_block_data (and the others mentioned above).

Expected behavior
I think all blocks should be passed through the same hooks (pre_render_block, render_block_data) - the same method should be used for nested and top-level blocks.

Editor version (please complete the following information):

  • WordPress version: 5.5
  • Does the website has Gutenberg plugin installed, or is it using the block editor that comes by default? default
  • If the Gutenberg plugin is installed, which version is it? -
[Feature] Blocks [Type] WP Core Bug

All 3 comments

I'm experiencing the same issue as well! Would be great if someone could have a look at this.

@noisysocks Any information as to what exactly needs testing? I just spun up the current state of the gutenberg plugin via wp-env and it's still the same. It's also seeable from code as I've described in the OP.

Hey @gaambo! We use Needs Testing for tickets that require a little bit of setup in order to reproduce. The label encourages folks to come back to it e.g. in a triage session.

I just checked and was able to confirm this bug. pre_render_block, render_block_data and render_block_context are only called for top-level blocks which isn't what you would expect from reading those filters' documentation. We probably should be applying these filters inside WP_Block::_construct as well.

Since fixing this will require a Core change, I've moved this ticket into Core Trac: https://core.trac.wordpress.org/ticket/51708

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JohnPixle picture JohnPixle  路  3Comments

davidsword picture davidsword  路  3Comments

aaronjorbin picture aaronjorbin  路  3Comments

jasmussen picture jasmussen  路  3Comments

mhenrylucero picture mhenrylucero  路  3Comments