Skip to content

Blocks: Add support for block name aliases for variations #43743

Open
@gziolo

Description

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 have core/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:

// Convert derivative blocks such as 'core/social-link-wordpress' to the
// canonical form 'core/social-link'.
if ( name && name.indexOf( 'core/social-link-' ) === 0 ) {
// Capture `social-link-wordpress` into `{"service":"wordpress"}`
newAttributes.service = name.substring( 17 );
name = 'core/social-link';
}

// Convert derivative blocks such as 'core-embed/instagram' to the
// canonical form 'core/embed'.
if ( name && name.indexOf( 'core-embed/' ) === 0 ) {
// Capture `core-embed/instagram` into `{"providerNameSlug":"instagram"}`
const providerSlug = name.substring( 11 );
const deprecated = {
speaker: 'speaker-deck',
polldaddy: 'crowdsignal',
};
newAttributes.providerNameSlug =
providerSlug in deprecated
? deprecated[ providerSlug ]
: providerSlug;
// This is needed as the `responsive` attribute was passed
// in a different way before the refactoring to block variations.
if ( ! [ 'amazon-kindle', 'wordpress' ].includes( providerSlug ) ) {
newAttributes.responsive = true;
}
name = 'core/embed';
}

Benefits of using aliases

  • Aliased block names would get converted into unique class names assigned to block variations, giving more options to style their sites.
  • Manipulate in theme.json granularly (like attach custom CSS that only is used when the block is used, etc.).
  • Aliases used would give easier access for site owners to the content analysis of which blocks and variations are used in posts and templates.
  • Benefit from all the block API tools in existence (like 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 the product-list variation added to the core/query block.

Some examples to consider:

<!-- wp:template-part {"slug":"header","theme":"twentytwenty"} /-->
<!-- wp:meta { "name": "mykey", "type": "heading" } /-->

becomes

<!-- wp:template-part/header {"theme":"twentytwenty"} /-->
<!-- wp:meta/mykey { "type": "heading" } /-->

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

[Feature] Block APIAPI that allows to express the block paradigm.[Feature] Block VariationsBlock variations, including introducing new variations and variations as a feature[Status] In ProgressTracking issues with work in progress[Type] EnhancementA suggestion for improvement.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions