Add option to block API to only allow root level insertion
fabiankaegy opened this issue · 3 comments
What problem does this address?
There are instances where a block should only ever exist at the root level of the block editor. It should never be nested within a Group or any other InnerBlocks area. Examples of this are things like the Page Header Template Part. Also when building custom blocks this is something that comes up here and there.
Often this is best paired with the supports.multiple
option set to false so an editor can only insert the block once at the root level
What is your proposed solution?
Add a new Block Supports option that is essentially the opposite of ancestor
or parent
. Maybe it could even use the parent
key just set to false.
@fabiankaegy, thank for raising the issue. It would be great to double check if we already have something similar tracked in #41236. Otherwise we should add it to the list of tasks there.
Add a new Block Supports option that is essentially the opposite of ancestor or parent. Maybe it could even use the parent key just set to false.
It would be interesting to explore whether we can have more options for the parent
and ancestor
fields like:
core/post-content
– would be a good fit for blocks that make sense only in the post editor like the Table of Contents discussed in #42229core/template
– would be a good fit for Template Part that is implemented with #37157 (props to @Mamaduka for the link to PR)root
(or similar keyword) could be also an option forparent
In general, we would benefit of having a simple way to express that a given block should be available only for a given post type or template type. I'm positive that we can avoid introducing another field in Block API to fulfil the requirements.
I have a possible use case that might benefit from something like this.
I'm creating a block that can be used standalone on a custom post type, and also as an inner block of another block that is only available on pages. What I'd like to be able to do is display the block in the inserter of the custom post type, but not display it in the inserter of a page. Removing it from the block inserter is normally handled by adding a parent
attribute, but since this block is both an inner block and also not, depending on context, that approach doesn't really work.
This is now possible by setting the parent to core/post-content