WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.34k stars 4.13k forks source link

Standardise data access for blocks on the server #58510

Open draganescu opened 7 months ago

draganescu commented 7 months ago

This is a bit late in the game but I wanted to start this discussion better late than never:

Problem: on the server there are many ways to retrieve the data you need for a block. This leads to various code spaghetti in block rendering callbacks which then later hamper block API advancements.


Case in point: server side previews and pattern overrides are both stuck at showing placeholders for empty image blocks. Another block, the cover block, decides in its rendering function that a cover block displaying a featured image means it has an empty URL. Pretty arbitrary. The same goes for the featured image block which is a post showing an image but has no URL attribute defined.

Case in point: block hooks need to add feature specific code to block rendering callbacks to hook into the data these blocks fetch from their associated entities. The way to retrieve data if the block has controlled inner blocks is a free for all.


Solution:

use the incoming power features of block hooks and block bindings to solve this problem.

''How?'' By nudging block developers - including core contributors - to use standard methods to access required data:

Trade Offs:

Wins:

michalczaplinski commented 7 months ago

Hi @draganescu! 👋 Thanks for opening this discussion!

I must say that I'm not quite following here what the specific problem is. Could you articulate it in a different way with some examples?

server side previews and pattern overrides are both stuck at showing placeholders for empty image blocks.

I don't understand what you mean here. What should an empty image block render other than a placeholder?

If a block does not use exclusively attributes to represent the data they render they’ll have problems with binding, because the binding will not happen as a last action indifferent of the render callback’s soup.

I've read it a few times but I still don't understand 🙁 . What do you mean by "last action indifferent of the render callback’s soup"?

make block bindings only update attribute values from sources and leave the rendering to the block rendering callback. That means let’s not do https://github.com/WordPress/wordpress-develop/pull/5888/files#diff-c569b1ecb11a007a563788a481710b51e0cb21cd1d1885490bf9c91777842f23R297 this part and instead offer a helper function for replacing markup in static blocks from HTML sourced attributes.

Please correct me if I misunderstood. You're suggesting removing the replace_html() function from its current place and leaving the responsibility of rendering a blocks with updated attribute values (the values coming from a block binding source) to that block itself. Specifically, the block would have to use some helper to update the static block's HTML.

The problem I see with this approach is that whenever a value coming from a source changes, the value in the static block would not be updated automatically. The user would have to e.g. re-save the post. How do you think this could be overcome?