Repository navigation
Description
Extend the concept of inner blocks to support a more direct relationship between sets of blocks. This new addition of parent–child would be a subset of the inner block functionality. The premise is that certain blocks only make sense as children of another block.
Justification
Let's consider a block called Product that represents an item with various sub-elements Price, Name, Add to Cart, etc. These are treated as blocks that the developer wants to define but let users manipulate directly. They also want to use the native inner blocks functionality instead of recreating its interactions in ways that would be inconsistent.
The key difference with regular blocks is that these blocks (price, name, cart, etc) should not appear in the main inserter unless the user is currently within the parent Product block. In other words, these inner blocks are registered to only be available for inserting within a product block, and not elsewhere.
Implementation
The addition to the block API would be something like the following:
const name = 'plugin/product-price';
const settings = {
// array property with allowed parents
parent: [ 'plugin/product' ]
};A parent block would, in turn, be able to define these children as defaults, just like templates and InnerBlocks works.
Inserter
The inserter would have to be aware of the context to include (and exclude) these additional blocks when the occasion is right.
Consideration
There are similarities here with the idea of restricting a block to a CPT. In that case, the CPT is effectively the "parent" property. We might find a way to combine both — perhaps parent: [ 'post', 'page', 'plugin/product' ] allows to specify both post types and blocks in the same mechanism. Given blocks require a slash for namespace, we might be able to split them through that implicit mark.
Activity
markjaquith commentedon Mar 10, 2018
Very nice. I can think of a lot of uses for this. Will encourage parent blocks with more standard UI, and will reduce block mental overload.
Eh, if it is split out, it is immediately obvious what is going on.
I'm thinking that the inserter should prioritize child-blocks that explicitly mention a parent, when inserting into that parent. So adding to
Productwould show youPriceProduct Name,Add to Cartbefore anything else.felixarntz commentedon Mar 11, 2018
This will greatly improve the granularity with more specific blocks. One additional thing to consider I could think of: Child blocks of a product may wanna have the requirement of either a product block as parent or as a regular (non-child) block in a
productpost type. It may be too specific, but I think it is a common scenario as well (think about a dedicated product page vs embedding a product in another piece of content).mtias commentedon Mar 12, 2018
Yes, this was what I had in mind too. We probably should rename the first tab of the inserter to something like "Common", since it now takes both frequency and recency into account, and with this further contextual addition it would still be applicable as "common for the current context". cc @jasmussen
I think this would be covered by the
allowedBlockspart of template declaration (inverts control from child to parent). See #5452jasmussen commentedon Mar 12, 2018
First thing this made me think of, is our ticket to let users insert images inline: #2043 (comment).
Although there are nuances between this ticket and inserting images or other inline elements into paragraphs, it makes a lot of sense to think about the insertion UI in a cohesive way here.
mtias commentedon Mar 12, 2018
Definitely, the only difference is that other blocks are also valid in this case, so it's less about an entirely different mode and more about prioritizing child blocks somehow.
aduth commentedon Apr 5, 2018
For symmetry with existing functions like
register_taxonomy's second argument$object_type, we could consider aligning this as an argument of theregisterBlockTypefunction itself:noisysocks commentedon Apr 11, 2018
I'm going to take this on. There's some slight overlap between this feature and #5452 so first I'll help @jorgefilipecosta get that merged in. I'll then work on a quick proof of concept to serve as the base for a conversation about how we go about implementing this and the exact API.
We could probably deprecate
isPrivatein lieu of this, too. If one restricts a block from being inserted into everything, then the block essentially becomes private.What should happen if the
allowedBlockNamesproperty of a parent block (added in #5452) conflicts with theparentproperty of one of its child blocks?For example, if we had:
Can a user add a Add to cart block to the Product block?
That tab is named Suggested now which I think works well for this 🙂
mtias commentedon Apr 12, 2018
That's a good question. If we make
allowedBlockNameswin, it means that there's no easy way for a 3rd party block to become "available" in another 3rd party block.Which makes me think that
parent: [ 'plugin/product' ]should work as an addition to allowedBlockNames definition (unless filtered out).Shelob9 commentedon Apr 13, 2018
I'm very excited about this. I think that this fills a lot of the gaps in templates, that I talked with @gziolo about awhile back. For example, what if we want some blocks in a template to be required, but can be moved, or not required but if they are present they must be the first block, etc.
From @noisysocks comments:
I think it would be great if in the parent block, I could define basically like @noisysocks but also whitelist and blacklist child blocks.
So in this updated pseudo-code, the Add to Cart block can be added to Product block because it's in the array used for
allowedChildren, but it is not a there by default, because it's not part of the array used fordefaultChildern.noisysocks commentedon Apr 17, 2018
OK, hear me out.
The more I think about this more I think that a
parentproperty is the wrong approach. A few reasons:<InnerBlocks>components. This will be important as we head towards allowing folks to build complex layout blocks, e.g. a block that defines a content region, a sidebar region, and a footer region.parent,allowedBlockNamesandallowed_block_typesall represent the same concept: restricting what blocks can be inserted into a (sub)tree of blocks. But it's hard to reason about these three concepts simultaneously, because whereasallowedBlockNamestakes a top down view of the block tree,parenttakes a bottom up view.Proposal
My current thinking is that we could enhance
allowedBlockTypesto accept a function. To illustrate, let's run through some scenarios.1. A Photoset block that can contain only Image blocks
To whitelist blocks, we just specify a plain array:
2. An Aside block that can contain any block except itself
To blacklist blocks, we remove them from the passed array:
3. A Product block that can contain anything and Add To Cart blocks
To allow additional blocks, we add them to the passed array:
We can go further and tell Gutenberg that we think it ought to put the Add To Cart block in the Suggested tab1:
Going further still, we can tell Gutenberg to not allow the Add To Cart block to be inserted at the root level2:
Weird diagram
The nice thing about this design, I think, is that it's composable: each
allowedBlockTypes()function takes the result of its parent'sallowedBlockTypes(). You can e.g. insert an Add To Cart block in your Aside block so long as the Aside block is nested within a Product block.It's easy to reason about because you can picture your post as a tree of blocks, with the list of allowed block types "flowing" down from the root:
1 We could maybe infer this automatically based on what
allowedBlockNames()receives versus what it returns.2 This filter could replace
isPrivate, e.g.core/blockwould be excluded by default. It could also replace or compliment theallowed_block_typesPHP filter.lsl commentedon Apr 17, 2018
@noisysocks agreed a filter function is the best solution.
Regarding top down or bottom up I think the correct solution involves doing both:
InnerBlocks - should be able to control what content they contain - and they should get the final say - preferably with a simple way to add preference (As discussed above.)
Blocks in general - should be able to decide themselves whether they get included in the inserter given their current context. I haven't looked at how that could happen (or if its viable) but that seems like it gives you the most freedom.
Probably goes without saying but I would continue to keep both concepts separate they're similar in that they deal with the inserter but they goals are quite dissimilar.
Innerblock filters are more about controlling content. Where block level filters are more about creating context aware blocks.
Total hunch but I suspect doing block level filtering would only including changing isPrivate to an function and maybe renaming the name. Quite easy to handle any existing isPrivate settings that way as well.
brandonpayton commentedon Apr 18, 2018
@noisysocks I am still processing all of this but tend to agree with the general approach. I'm going to get a little philosophical because I do that sometimes. It's helpful for me but hopefully not just for me.
A top-down approach is natural like a house is built on a foundation. It allows a truth to naturally inform the system. In this case, that means allowing a parent block implementation to inform its contents. A bottom-up approach cannot wield the same authority because it does not form a foundation.
A bottom-up approach can co-exist with a top-down approach but only with authority granted by the top-down approach, and for me, it is easier to think about a top-down-only approach, similar to how React is easier to think about with unidirectional data flow.
Regarding mechanism:
This sounds good to me because it gives us the ability to use logic rather than programming-by-data structure using lists, but it seems like we need a way to express the truth of
allowedBlockTypeson the server since it is the source of truth (when Gutenberg is used within WordPress). I'm wondering whether this means we need to continue expressing allowed block types with lists.@mtias mentioned:
With a top-down-only approach, I am thinking that a plugin adding a 3rd-party block would need to use a filter hook to add the 3rd-party block as an allowed block.
Regarding composability:
What does this mean for a hypothetical Slideshow block that can contain only Slide blocks? We would want Slide blocks to allow many block types, but it sounds like that would be limited by the restriction on Slideshow.
5 remaining items
mtias commentedon Apr 30, 2018
@noisysocks apart from the
InnerBlocksclarification, I wanted to add that, even though the result is similar (filtering down which blocks are available within anInnerBlocksarea) the intention and expressiveness are different.A
parentproperty is a child declaring what their context is and saying "I should not be treated as a root block" or as part of the default set of available inner blocks in other blocks (like Columns). This has the advantage of not depending on filtering the parent's functionality — the parent doesn't need to have any specific knowledge of this child block.allowedBlocks, however, is about the parent restricting what can be inserted within itself. It's an API the block author can directly control.We are going to need both.
gziolo commentedon Apr 30, 2018
How do we plan to solve the case when
Parent Block Aallows onlyBlock Bas children andChild Block Callows onlyParent Block Aas the parent?markjaquith commentedon Apr 30, 2018
@gziolo specifying a relationship should trump not specifying a relationship.
ZebulanStanphill commentedon Apr 30, 2018
@gziolo I would say that in that situation,
Child Block Cwould simply be unable to be inserted anywhere. To get around this (in the context of a plugin/theme adding a child block to a block from WordPress core or another plugin/theme that usually only has one allowed child), you should be able to explicitly override the list of allowed children of a block just like you can override the edit/save functions of any block.noisysocks commentedon May 2, 2018
This was answered in #5540 (comment). In my proof of concept I made it so that, in this case,
Block Cis insertable intoParent Block A. The plugin I created to help test the proof of concept has an example of this.gziolo commentedon May 2, 2018
which is:
Yes, this is one way of solving it, but it makes it harder to understand how
allowedBlockNamesproperty work. In that case it should be reallyallowedBlockNamesWithoutChildrenThatOptInExplicitly:)I think the same question applies to Templates, we need to have well-defined priorities for every solution that wants to modify what should be allowed in a given context.
There is one drawback I can envision with this approach. When a site owner would want to disallow multiple individual Child blocks to be exposed in the inserter of the given parent block, they would have to update all such blocks one by one. We need to keep that in mind that it is going to be easier for those who create Child blocks, but not always to those who want to keep parent blocks isolated.
noisysocks commentedon May 3, 2018
Been thinking about this.
To handle all of these cases we're identifiying, our API needs to be expressive enough such that a block can specify:
To this end, I think what would work is if we introduce the concept of an allow list. This is an object that maps block types to whether or not they are explicitly allowed as a parent or child. A wildcard (
*) case determines whether or not parents or children are allowed in general.Some illustrative examples
1. A Photoset block that can contain only Image blocks
2. An Aside block that can contain any block except itself
3. An Add To Cart block that can only be added to Product blocks
Some formality
[ 'foo', 'bar' ]is shorthand for{ 'foo': true, 'bar': true, '*': false }.trueandundefinedare shorthand for{ '*': true }.falseis shorthand for{ '*': false }.The logic for determining whether or not a child can be inserted into a parent is:
jorgefilipecosta commentedon May 3, 2018
Hi, @noisysocks,
I really like the directions we are taking and the solution you proposed in #5540 (comment).
In my option we should, in fact, have three levels of restriction:
Besides that, I think we should have hooks on the three levels of restriction that allow the settings to be changed.
So, if I'm creating a block equivalent to 'acme/product' and I also want to support acme/add-to-cart inside my block, I can hook into registerBlockType and change the way acme/add-to-cart is registered to allow it to be nested inside my block. I think there will be no additional work here as we can use the existing hooks to extend the block registration.
The contrary should also be possible if a parent block sets some restriction and I'm creating a block equivalent to one of the child blocks, I should be able to use a hook and allow my block to be nested inside. We will need to create a new hook for this.
noisysocks commentedon May 3, 2018
You could also, with the advanced allow list syntax, set
allowedChildren={ { 'acme/add-to-cart': true, '*': true } }on the<InnerBlocks>inacme/product.But yes, I agree, hooks would provide a good API for doing really advanced (e.g. programatic) things with these allow lists.
gziolo commentedon May 4, 2018
It would be great to implement the same syntax on PHP side for
allowed_block_types. At the moment it supports only whitelisting by providing the list of block names.strarsis commentedon May 5, 2018
This would be a very useful feature! Can I emulate this already?
When this is not possible yet, can I repeat a component inside that can hold blocks by itself?
Edit: Related issue: #6607
mtias commentedon May 7, 2018
You can use the
InnerBlockscomponent in your blocks.strarsis commentedon May 7, 2018
@mtias: Right. But for the slider block (see referenced issue), how can an user simply click inside for a new slide(slide, not slider) block? Currently the user can click on a slider block (which nests a new slider) or an image, but the other elements aren't offered.
Also the amount of available sub-blocks has to be configured by user first, it doesn't just grow like the top level Gutenberg editor.