-
Notifications
You must be signed in to change notification settings - Fork 4k
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: Add support for block name aliases for variations #43743
Comments
Hey @gziolo has there been any more progress around this idea/issue?
*This will depend if the above proposed solution will also work with block hooks. A current example would be: |
I guess this would also allow setting allowedBlocks/template on innerBlocks to allow only some variations inside it? For Example we're using a variation of core-media/text to show a team member with some data - the innerblocks of the core-media/text block are locked. We then have a group block as section in which editors can insert these blocks. At the moment we've locked the group block to allow only core/media-text blocks, but of course the editors could also insert a non-team-member variation of the media-text blocks. |
Potentially. It didn't occur to me initially, but now that you said it, it's worth considering as that would be a very interesting use case. It would also play nicely with block variations that define block bindings, so potential Site Title and Post Title could be replicated with the Heading block but maybe people still would want to have a control where to insert it. |
I'm still in the process of understanding the complexities in this since this code is unchartered territory for me, but having given this a little bit of thought and there are a few things I think we need to consider, unless I have over complicated it:
|
Variations can be registered client side with
I'm not sure if I misunderstood something here, but I don't think there is a difference in handling between client and server. In both cases we treat all variations of a block as the same single block. The difference is that with variations we have some selected attributes or inner blocks, only when we first insert them. After insertion we can update attributes and can end up not 'having' the original variation we inserted. This could be done either from UI with the block variation transforms or by simple updating the specific attributes in the markup. I think this proposal is for adding Some examples that we need that is global styles and theme.json to add specific styles for specific block variations or in block manager to be able to disable/enable specific variations of the block and not holistically do that for the main block(allow only It's a complex issue and I think some code explorations will start showing the way forward. I think we can do this iteratively and not update all the UI we would need it eventually. For start we should explore how we can update the registration of blocks and block variations to work with an alias and what an alias would be technically. For example what do we do with the |
RequirementsOne of the major questions is where we would use block variation aliases instead of the underlying, „canonical“ block names, and where we would use them in addition. This has been partly answered by @gziolo who mentioned the following goals:
OTOH, we're planning to retain the canonical block name in the editor, which means that there will have to be some logic to convert the alias (as found in the persisted markup in the DB) to the canonical name upon loading in the editor, and back upon saving. I've shared some thoughts on whether we need to include the alias in the markup in the editor (e.g. in block In conclusion, we'll store the block alias in the persisted markup in the DB, but will convert it to its canonical name in the editor. However, we still need to answer the question of whether we'll use the alias or the canonical names for all of the in-between steps (e.g. in the parsed block format or in Server-side APIA @ntsekouras mentioned, there’s an existing API on the server side to register block variations ( Furthermore, variations already have a name field to distinguish them from each other. For example, the IMO, there’s a risk of confusion if we introduce aliases (which would be typically prefixed — think (Note that this would require us to change the parser, and would pose another challenge described in more detail here.) Auto-generating aliases from block type names and variation names has the drawback that 3rd-party plugins cannot use their own namespace for variations they add to existing blocks; e.g. #43743 had the example of a On the plus side, we already seem to be creating class names for block variations that closely match the pattern we're considering for the aliases. For example, the Finally, note that using "auto-generated" aliases is also partly motivated by YAGNI -- it doesn't preclude allowing manual setting of aliases later. To keep the API simple, I'd add an Backwards-compatibility and consistency of layersOne of the trickiest problems with using block variation aliases is the following dilemma: On the one hand, we want to expose them to extenders so that they can write code that targets them much like they would otherwise target canonical block names -- e.g. for Block Hooks to be able to inject a block variation. On the other hand, if we start exposing the variation name instead of the canonical name in certain places, this is likely going to break existing code. I'll give an example. For Block Hooks' array(
'blockName' => 'core/social-link/wordpress'
) (rather than setting the IMO, this is fine: The parsed block format is a fairly low-level representation of the markup, generated by the block parser. (Note that we also likely don't want to introduce a dedicated However, what about existing code that relied on the canonical block name -- e.g. a As a direct consequence, with the constraints above, we cannot automatically opt in all blocks that have variations to using auto-generated aliases in the persisted markup; instead, we'll have to make it a per-block user choice, likely through an The next layer where we need to decide how to represent aliases is Anyway, that's the current state of my research and thinking. Curious to hear your thoughts, especially from the folks who've worked with variations before @gziolo @ntsekouras. LMK if you agree with my assumptions and conclusions, and if I'm missing something 😄 |
Thank you @ockham for documenting this so thoroughly! Something I want to pull out for discussion which I've brought up in DM with you is the following statement:
I'd still be interested to see how this might work, I am currently under the assumption that this means that we can compare the current blocks attributes against all registered variation blocks default attributes to determine whether it's a variation or not (reminder; one of the main motivators for variations existing is allowing a user to create multiple versions of the same block with different attribute values). With that in mind, if the user inserts a variation and changes an attribute it would fall back to the canonical block type and wouldn't get converted upon serialization and persisted to the database. I'm imaging scenarios where we want to insert the same variation with slightly different attributes, imagine the scenario where we create a variation of WooCommerce's Product Collection block (e.g. Obviously this may only be an issue if I've understood what you mean correctly, or perhaps there's a way around this 😄 |
@tjcafferkey So I'd definitely wouldn't want the variation to become unrecognized if the user changes only some circumstantial attribute(s). But I thought we'd be safe from that happening as we'd only evaluate "distinguishing"/"defining" attributes? There aren't a lot of examples of server-side registered variations in the WP and/or GB codebase, but here's one: The Post Terms block registers a variation for each known taxonomy (so one for Categories, one for Tags, etc?). The defining attribute is the block's gutenberg/packages/block-library/src/post-terms/index.php Lines 95 to 97 in 7674a57
This means that if we're given a Post Term block (no matter if as markup, parsed, or So the algorithm I'm envisioning would do something like this:
Or so. Maybe I'll give that a try somewhere, e.g. over at WordPress/wordpress-develop#6594 😬 |
|
Ah, I see now the way in which you're thinking about this and I like the idea! One thing I just want to mention is that you could have a block variation whose attributes remain the same as the canonical block but its inner blocks are different. Just pulling out this sentence from the API documentation for reference.
From my understanding your approach should still work but we'd also need to run a comparison against |
Oh, that's a very good point, thank you! I just had another look at the docs myself, with a focus on the
Of course we cannot use a JS function like
That's a format that we could actually use (and evaluate) on the server side! We might want to give precedence to that |
Makes sense. Although it is my understand that callbacks assigned to the
|
Update: Based on our research and experiments, we're planning to generate block aliases by concatenating block name and variation (i.e. the Block authors will need to opt in to have aliases generated for their blocks, for reasons given here. We have a prototype that seems to be working reasonably well in WordPress/wordpress-develop#6594 (which is based on prior art by @tjcafferkey). We'll work to polish that PR and get it ready for inclusion in 6.6. Since this will affect the way that block markup is stored and processed, there's a risk that this will meet some opposition, or that it might impact existing code (both Core and 3rd party) in a way that we're not yet aware of. To deal with that risk, we'll break the problem into multiple parts, and will frontload the less risky parts, as they're required for the riskier parts anyway, and have a higher chance to make it into 6.6:
|
What problem does this address?
Part of Block API roadmap #41236.
Let's explore whether some block variations can be read as a block type. For example, instead of
core/template-part/header
be able to just havecore/header
to still get resolved by the template part functions. We used to have a similar granular naming schema for Embed and Social Link blocks before we converted them to block variations:gutenberg/packages/blocks/src/api/parser/convert-legacy-block.js
Lines 23 to 29 in bdb6a41
gutenberg/packages/blocks/src/api/parser/convert-legacy-block.js
Lines 31 to 50 in bdb6a41
Benefits of using aliases
theme.json
granularly (like attach custom CSS that only is used when the block is used, etc.).multiple
block support).What is your proposed solution?
Add handling for block type name aliases used with block variations. They should fall back if necessary to the original block type name, e.g.
woocommerce/product-list
that can get resolved to theproduct-list
variation added to thecore/query
block.Some examples to consider:
becomes
This could also remove the need for complex logic used with block variations when detecting the variation based on the result of the
isActive
callback executed.The text was updated successfully, but these errors were encountered: