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
Blocks: Filterable allowed inserter block types #3577
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3577 +/- ##
==========================================
+ Coverage 35.29% 35.34% +0.05%
==========================================
Files 267 267
Lines 6744 6762 +18
Branches 1221 1226 +5
==========================================
+ Hits 2380 2390 +10
- Misses 3687 3694 +7
- Partials 677 678 +1
Continue to review full report at Codecov.
|
After reading the proposed documentation in #3567, it seems we have a few other options for limiting the types of blocks supported:
cc @youknowriad |
Yes, we do but I do see the value of setting the block types in the "context", I was originally thinking we should add them to I'm not sure I understand why you can't use this in the -- So if I understand properly, this only affects the Inserter and still use the globally registered blocks (and for example pasting, converting can still make use of these block types). I think it's a good first step but I'd love if we can avoid to use the globally registered blocks at all, and just provide the available blocks in the context. I tried this in one of my previous PRs, it could work but it's a way bigger change. I think this could also be valuable for nesting as well where we'd want to limit the available blocks. |
Not so much that it couldn't be done. Something about the editor consuming components from blocks didn't feel as clean as referencing the registration. |
This pull request seeks to enable filtering of block types offered through the Inserter component. It does so both in the client and in the server.
On the server, a plugin author can filter
allowed_block_types
to return eithertrue
(all block types supported),false
(no block types supported), or an array of block type names to allow. This may enable custom post types to limit the types of blocks allowed.In the client, since the block types are passed through as a setting in
EditorProvider
, creating a new provider context with settings specifying block types is sufficient to limit permitted block types. This will become relevant for block nesting to allow a block to define allowed children types.Implementation notes:
The behavior here required that an editor component gain access to
editor
context. The existingwithEditorSettings
higher-order component would have worked, but was implemented expecting to be used only in the context of theblocks
module. With the changes here, I have movedwithEditorSettings
to a more genericwithContext
higher-order component in the components module, allowing a component to opt-in to receiving context of any name.Testing instructions:
Ensure unit tests pass:
Verify that you can filter block types, e.g. adding filters to one of Gutenberg's PHP files:
Disable all block types (no inserter is shown):
Restrict block types: