Skip to content
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

Docs: Add register_block_type_args as a way to filter registration of a block in the documentation #54112

Open
tresorama opened this issue Sep 1, 2023 · 0 comments
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Developer Documentation Documentation for developers

Comments

@tresorama
Copy link

tresorama commented Sep 1, 2023

When the dev need this docs ?

When creating a plugin that add new attributes to blocks that the plugin itself doesn't control (example are core blocks or blocks from other plugins), dev uses some filter hooks in PHP and React side to accomplish the goal.

Why current situation is not optimal

Currently, this docs https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#registration
does include some hooks used to alter the regitration phase of a block.

But it lacks the PHP filter register_block_type_args that is mandatory to use for a plugin that add attributes to block.

Additional Notes

While doing a plugin that add "attributes" to all blocks I faced some obstacles, that documentation could improve to help other with same requirements.

To add a new "attribute" to a block you need to add UI to the editor, used for controlling the new attribute.
This is done in the React side (with editor.BlockEdit filter hook) and wrapper component that setAttributes.

Doing that is ok from the "funcitonal" side (it works) but is not optimal and its fragile.
You can encounter "validation" error with some blocks, because while you are using the Editor, the editor itself transmit block data to PHP server to perform validation of attributes.
The validation DOESN'T ALLOW unknown attributes, so often validation fails and you have a "broken" blocks in the editor.

It is good to say that often the validation fails, the block is broken on the editor, but it is rendered correctly in the frontend.

Maybe in future it will be good to ALLOW UNKNOWN ATTRIBUTES ,but i don't know the internals of the system...

Going back to topic, the validation uses the schema of the registration phase to do its job, so it is mandatory that you will add your "attributes" in the registration.

The registration phase of block can happens both in PHP or React side, so the dev needs to augment the block schema in both sides.
In JS, dev uses blocks.registerBlockType filter hook.
In PHP, dev uses block_type_metadata filter hook.

All these hooks are present in the documentation https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#registration

But i encounterd again the validation error with some thirdy party blocks, and after a debug session i found that , in PHP side, is better to replace block_type_metadata usage with register_block_type_args.
The React side is ok.

@bph bph added the [Type] Developer Documentation Documentation for developers label Sep 1, 2023
@gziolo gziolo added [Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience labels Sep 2, 2023
@gziolo gziolo changed the title docs: Add register_block_type_args as a way to filter registration of a block in the documentation Docs: Add register_block_type_args as a way to filter registration of a block in the documentation Sep 5, 2023
@gziolo gziolo mentioned this issue Sep 5, 2023
58 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Developer Documentation Documentation for developers
Projects
Development

No branches or pull requests

3 participants