From fb21722ea56cb7b00ac7786b40c577487f3d2dcf Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Fri, 26 Mar 2021 13:58:27 +0100 Subject: [PATCH 1/8] Blocks: Introduce registerBlockTypeFromMetadata API --- packages/block-library/src/index.js | 8 ++--- packages/block-library/src/index.native.js | 12 ++++--- packages/blocks/README.md | 19 ++++++++++ packages/blocks/src/api/index.js | 1 + packages/blocks/src/api/registration.js | 40 ++++++++++++++++++++++ 5 files changed, 70 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index e1b6d279acaad..22b4385b9a806 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -4,12 +4,11 @@ import '@wordpress/core-data'; import '@wordpress/block-editor'; import { - registerBlockType, + registerBlockTypeFromMetadata, setDefaultBlockName, setFreeformContentHandlerName, setUnregisteredTypeHandlerName, setGroupingBlockName, - unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase } from '@wordpress/blocks'; /** @@ -106,10 +105,7 @@ const registerBlock = ( block ) => { return; } const { metadata, settings, name } = block; - if ( metadata ) { - unstable__bootstrapServerSideBlockDefinitions( { [ name ]: metadata } ); - } - registerBlockType( name, settings ); + registerBlockTypeFromMetadata( { name, ...metadata }, settings ); }; /** diff --git a/packages/block-library/src/index.native.js b/packages/block-library/src/index.native.js index ba0d20795a111..850520750ecd8 100644 --- a/packages/block-library/src/index.native.js +++ b/packages/block-library/src/index.native.js @@ -10,6 +10,7 @@ import { sortBy } from 'lodash'; import { hasBlockSupport, registerBlockType, + registerBlockTypeFromMetadata, setDefaultBlockName, setFreeformContentHandlerName, setUnregisteredTypeHandlerName, @@ -127,10 +128,13 @@ const registerBlock = ( block ) => { return; } const { metadata, settings, name } = block; - registerBlockType( name, { - ...metadata, - ...settings, - } ); + registerBlockTypeFromMetadata( + { + name, + ...metadata, + }, + settings + ); }; /** diff --git a/packages/blocks/README.md b/packages/blocks/README.md index 8adbe8d9dee9d..8d93042880b32 100644 --- a/packages/blocks/README.md +++ b/packages/blocks/README.md @@ -713,6 +713,25 @@ _Returns_ - `?WPBlock`: The block, if it has been successfully registered; otherwise `undefined`. +# **registerBlockTypeFromMetadata** + +Registers a new block provided from metadata stored in `block.json` file. +It uses `registerBlockType` internally. + +_Related_ + +- registerBlockType + +_Parameters_ + +- _metadata_ `Object`: Block metadata loaded from `block.json`. +- _metadata.name_ `string`: Block name. +- _additionalSettings_ `Object`: Additional block settings. + +_Returns_ + +- `?WPBlock`: The block, if it has been successfully registered; otherwise `undefined`. + # **registerBlockVariation** Registers a new block variation for the given block type. diff --git a/packages/blocks/src/api/index.js b/packages/blocks/src/api/index.js index 5fdd8aad97bdf..4d535589b09ab 100644 --- a/packages/blocks/src/api/index.js +++ b/packages/blocks/src/api/index.js @@ -108,6 +108,7 @@ export { getCategories, setCategories, updateCategory } from './categories'; // children of another block. export { registerBlockType, + registerBlockTypeFromMetadata, registerBlockCollection, unregisterBlockType, setFreeformContentHandlerName, diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 81e27faab26f2..dabbc087d7384 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -310,6 +310,46 @@ export function registerBlockType( name, settings ) { return settings; } +/** + * Registers a new block provided from metadata stored in `block.json` file. + * It uses `registerBlockType` internally. + * + * @see registerBlockType + * + * @param {Object} metadata Block metadata loaded from `block.json`. + * @param {string} metadata.name Block name. + * @param {Object} additionalSettings Additional block settings. + * + * @return {?WPBlock} The block, if it has been successfully registered; + * otherwise `undefined`. + */ +export function registerBlockTypeFromMetadata( + { name, ...metadata }, + additionalSettings +) { + const allowedFields = [ + 'title', + 'category', + 'parent', + 'icon', + 'description', + 'keywords', + 'attributes', + 'providesContext', + 'usesContext', + 'supports', + 'styles', + 'example', + 'apiVersion', + ]; + + unstable__bootstrapServerSideBlockDefinitions( { + [ name ]: pick( metadata, allowedFields ), + } ); + + return registerBlockType( name, additionalSettings ); +} + /** * Registers a new block collection to group blocks in the same namespace in the inserter. * From 2dfdbdd02d67d6fcf37f729aa3937e9b8aefcebb Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Mon, 29 Mar 2021 12:28:13 +0200 Subject: [PATCH 2/8] Add i18n schema for block.json --- packages/blocks/src/api/i18n-block.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 packages/blocks/src/api/i18n-block.json diff --git a/packages/blocks/src/api/i18n-block.json b/packages/blocks/src/api/i18n-block.json new file mode 100644 index 0000000000000..9b44a6ec078ae --- /dev/null +++ b/packages/blocks/src/api/i18n-block.json @@ -0,0 +1,12 @@ +{ + "title": "block title", + "description": "block description", + "keywords": [ + "block keyword" + ], + "styles": [ + { + "label": "block style label" + } + ] +} From 80c27f2fab4be3e5acdc2ed9692725421a3a94ad Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Tue, 30 Mar 2021 11:19:32 +0200 Subject: [PATCH 3/8] Docs: Update documentation for metadata and internationalization --- .../block-api/block-metadata.md | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/docs/reference-guides/block-api/block-metadata.md b/docs/reference-guides/block-api/block-metadata.md index 35e014595b0c8..6a4160906ba3b 100644 --- a/docs/reference-guides/block-api/block-metadata.md +++ b/docs/reference-guides/block-api/block-metadata.md @@ -438,7 +438,7 @@ return array( ## Internationalization -WordPress string discovery automatically will translate fields marked in the documentation as translatable using the `textdomain` property when specified in the `block.json` file. In that case, localized properties will be automatically wrapped in `_x` function calls on the backend of WordPress when executing `register_block_type_from_metadata`. These translations are added as an inline script to the `wp-block-library` script handle in WordPress core or to the plugin's script handle. +WordPress string discovery system can automatically translate fields marked in this document as translatable. First, you need to set the `textdomain` property in the `block.json` file that provides block metadata. **Example:** @@ -451,19 +451,40 @@ WordPress string discovery automatically will translate fields marked in the doc } ``` -The way `register_block_type_from_metadata` processes translatable values is roughly equivalent to: +### PHP + +In PHP, localized properties will be automatically wrapped in `_x` function calls on the backend of WordPress when executing `register_block_type_from_metadata`. These translations get added as an inline script to the plugin's script handle or to the `wp-block-library` script handle in WordPress core. + +The way `register_block_type_from_metadata` processes translatable values is roughly equivalent to the following code snippet: ```php _x( 'My block', 'block title', 'my-plugin' ), 'description' => _x( 'My block is fantastic!', 'block description', 'my-plugin' ), - 'keywords' => array( _x( 'fantastic', 'block keywords', 'my-plugin' ) ), + 'keywords' => array( _x( 'fantastic', 'block keyword', 'my-plugin' ) ), ); ``` Implementation follows the existing [get_plugin_data](https://codex.wordpress.org/Function_Reference/get_plugin_data) function which parses the plugin contents to retrieve the plugin’s metadata, and it applies translations dynamically. +### JavaScript + +In JavaScript, you need to use `registerBlockTypeFromMetadata` method from `@wordpress/blocks` package to process loaded block metadata. All localized properties get automatically wrapped in `_x` (from `@wordpress/i18n` package) function calls similar to how it works in PHP. + +**Example:** + +```js +import { registerBlockTypeFromMetadata } from '@wordpress/blocks'; +import Edit from './edit'; +import metadata from './block.json'; + +registerBlockTypeFromMetadata( metadata, { + edit: Edit, + // ...other client-side settings +} ); +``` + ## Backward Compatibility The existing registration mechanism (both server side and frontend) will continue to work, it will serve as low-level implementation detail for the `block.json` based registration. From 304eba2e0b12aeed7e5bf58015f420c0858d2adb Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Tue, 30 Mar 2021 11:36:48 +0200 Subject: [PATCH 4/8] Test: Add test for registerBlockTypeFromMetadata --- packages/blocks/src/api/test/registration.js | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 3ef1c4713f814..19bdfc0bfc459 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -17,6 +17,7 @@ import { blockDefault as blockIcon } from '@wordpress/icons'; */ import { registerBlockType, + registerBlockTypeFromMetadata, registerBlockCollection, unregisterBlockCollection, unregisterBlockType, @@ -802,6 +803,40 @@ describe( 'blocks', () => { } ); } ); + describe( 'registerBlockTypeFromMetadata', () => { + test( 'registers block from metadata', () => { + const Edit = () => 'test'; + const block = registerBlockTypeFromMetadata( + { + name: 'test/block-from-metadata', + title: 'Block from metadata', + category: 'text', + icon: 'palmtree', + }, + { + edit: Edit, + save: noop, + } + ); + expect( block ).toEqual( { + name: 'test/block-from-metadata', + title: 'Block from metadata', + category: 'text', + icon: { + src: 'palmtree', + }, + keywords: [], + attributes: {}, + providesContext: {}, + usesContext: [], + supports: {}, + styles: [], + edit: Edit, + save: noop, + } ); + } ); + } ); + describe( 'registerBlockCollection()', () => { it( 'creates a new block collection', () => { registerBlockCollection( 'core', { title: 'Core' } ); From e236e1ec2638d6f7daed2cbebea75a104c797cd9 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Tue, 13 Apr 2021 16:11:10 +0200 Subject: [PATCH 5/8] Blocks: Translate metadata loaded from block.json --- packages/blocks/README.md | 1 + packages/blocks/src/api/registration.js | 82 ++++++++++++++++++-- packages/blocks/src/api/test/registration.js | 44 +++++++++++ 3 files changed, 122 insertions(+), 5 deletions(-) diff --git a/packages/blocks/README.md b/packages/blocks/README.md index 8d93042880b32..3d39a3651ae25 100644 --- a/packages/blocks/README.md +++ b/packages/blocks/README.md @@ -726,6 +726,7 @@ _Parameters_ - _metadata_ `Object`: Block metadata loaded from `block.json`. - _metadata.name_ `string`: Block name. +- _metadata.textdomain_ `string`: Textdomain to use with translations. - _additionalSettings_ `Object`: Additional block settings. _Returns_ diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index dabbc087d7384..993652cbf5192 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -5,9 +5,13 @@ */ import { camelCase, + isArray, + isEmpty, isFunction, isNil, + isObject, isPlainObject, + isString, mapKeys, omit, pick, @@ -25,6 +29,7 @@ import { blockDefault } from '@wordpress/icons'; /** * Internal dependencies */ +import i18nBlockSchema from './i18n-block.json'; import { isValidIcon, normalizeIconObject } from './utils'; import { DEPRECATED_ENTRY_KEYS } from './constants'; import { store as blocksStore } from '../store'; @@ -310,21 +315,73 @@ export function registerBlockType( name, settings ) { return settings; } +/** + * Translates block settings provided with metadata using the i18n schema. + * + * @param {string|string[]|Object[]} settingSchema I18n schema for the block setting. + * @param {string|string[]|Object[]} settingValue Value for the block setting. + * @param {string} textdomain Textdomain to use with translations. + * + * @return {string|string[]|Object[]} Translated setting. + */ +function translateBlockSettingUsingI18nSchema( + settingSchema, + settingValue, + textdomain +) { + if ( isString( settingSchema ) && isString( settingValue ) ) { + return settingValue + ':' + settingSchema + ':' + textdomain; + } + if ( + isArray( settingSchema ) && + ! isEmpty( settingSchema ) && + isArray( settingValue ) + ) { + return settingValue.map( ( value ) => + translateBlockSettingUsingI18nSchema( + settingSchema[ 0 ], + value, + textdomain + ) + ); + } + if ( + isObject( settingSchema ) && + ! isEmpty( settingSchema ) && + isObject( settingValue ) + ) { + return Object.keys( settingValue ).reduce( ( accumulator, key ) => { + if ( ! settingSchema[ key ] ) { + accumulator[ key ] = settingValue[ key ]; + return accumulator; + } + accumulator[ key ] = translateBlockSettingUsingI18nSchema( + settingSchema[ key ], + settingValue[ key ], + textdomain + ); + return accumulator; + }, {} ); + } + return settingValue; +} + /** * Registers a new block provided from metadata stored in `block.json` file. * It uses `registerBlockType` internally. * * @see registerBlockType * - * @param {Object} metadata Block metadata loaded from `block.json`. - * @param {string} metadata.name Block name. - * @param {Object} additionalSettings Additional block settings. + * @param {Object} metadata Block metadata loaded from `block.json`. + * @param {string} metadata.name Block name. + * @param {string} metadata.textdomain Textdomain to use with translations. + * @param {Object} additionalSettings Additional block settings. * * @return {?WPBlock} The block, if it has been successfully registered; * otherwise `undefined`. */ export function registerBlockTypeFromMetadata( - { name, ...metadata }, + { name, textdomain, ...metadata }, additionalSettings ) { const allowedFields = [ @@ -343,8 +400,23 @@ export function registerBlockTypeFromMetadata( 'apiVersion', ]; + const settings = pick( metadata, allowedFields ); + + if ( textdomain ) { + Object.keys( i18nBlockSchema ).forEach( ( key ) => { + if ( ! settings[ key ] ) { + return; + } + settings[ key ] = translateBlockSettingUsingI18nSchema( + i18nBlockSchema[ key ], + settings[ key ], + textdomain + ); + } ); + } + unstable__bootstrapServerSideBlockDefinitions( { - [ name ]: pick( metadata, allowedFields ), + [ name ]: settings, } ); return registerBlockType( name, additionalSettings ); diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 19bdfc0bfc459..7fb0c9cc0c9b6 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -835,6 +835,50 @@ describe( 'blocks', () => { save: noop, } ); } ); + test( 'registers block from metadata with translation', () => { + const Edit = () => 'test'; + const block = registerBlockTypeFromMetadata( + { + name: 'test/block-from-metadata-i18n', + title: 'I18n title from metadata', + description: 'I18n description from metadata', + keywords: [ 'i18n', 'metadata' ], + styles: [ + { + label: 'i18n-metadata', + title: 'I18n Metadata', + }, + ], + textdomain: 'i18n', + icon: 'palmtree', + }, + { + edit: Edit, + save: noop, + } + ); + expect( block ).toEqual( { + name: 'test/block-from-metadata-i18n', + title: 'I18n title from metadata', + description: 'I18n description from metadata', + icon: { + src: 'palmtree', + }, + keywords: [ 'i18n', 'metadata' ], + attributes: {}, + providesContext: {}, + usesContext: [], + supports: {}, + styles: [ + { + label: 'i18n-metadata', + title: 'I18n Metadata', + }, + ], + edit: Edit, + save: noop, + } ); + } ); } ); describe( 'registerBlockCollection()', () => { From e89312e79469c736b2292bc3a4efb22fd4e26c88 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Thu, 22 Apr 2021 13:39:50 +0200 Subject: [PATCH 6/8] Make fields translatable --- packages/blocks/src/api/registration.js | 24 ++++++++-------- packages/blocks/src/api/test/registration.js | 29 ++++++++++++++------ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 993652cbf5192..7a4e0f901e46f 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -24,6 +24,7 @@ import { */ import { applyFilters } from '@wordpress/hooks'; import { select, dispatch } from '@wordpress/data'; +import { _x } from '@wordpress/i18n'; import { blockDefault } from '@wordpress/icons'; /** @@ -318,45 +319,46 @@ export function registerBlockType( name, settings ) { /** * Translates block settings provided with metadata using the i18n schema. * - * @param {string|string[]|Object[]} settingSchema I18n schema for the block setting. + * @param {string|string[]|Object[]} i18nSchema I18n schema for the block setting. * @param {string|string[]|Object[]} settingValue Value for the block setting. * @param {string} textdomain Textdomain to use with translations. * * @return {string|string[]|Object[]} Translated setting. */ function translateBlockSettingUsingI18nSchema( - settingSchema, + i18nSchema, settingValue, textdomain ) { - if ( isString( settingSchema ) && isString( settingValue ) ) { - return settingValue + ':' + settingSchema + ':' + textdomain; + if ( isString( i18nSchema ) && isString( settingValue ) ) { + // eslint-disable-next-line @wordpress/i18n-no-variables, @wordpress/i18n-text-domain + return _x( settingValue, i18nSchema, textdomain ); } if ( - isArray( settingSchema ) && - ! isEmpty( settingSchema ) && + isArray( i18nSchema ) && + ! isEmpty( i18nSchema ) && isArray( settingValue ) ) { return settingValue.map( ( value ) => translateBlockSettingUsingI18nSchema( - settingSchema[ 0 ], + i18nSchema[ 0 ], value, textdomain ) ); } if ( - isObject( settingSchema ) && - ! isEmpty( settingSchema ) && + isObject( i18nSchema ) && + ! isEmpty( i18nSchema ) && isObject( settingValue ) ) { return Object.keys( settingValue ).reduce( ( accumulator, key ) => { - if ( ! settingSchema[ key ] ) { + if ( ! i18nSchema[ key ] ) { accumulator[ key ] = settingValue[ key ]; return accumulator; } accumulator[ key ] = translateBlockSettingUsingI18nSchema( - settingSchema[ key ], + i18nSchema[ key ], settingValue[ key ], textdomain ); diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 7fb0c9cc0c9b6..ead44e97c1021 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -8,7 +8,7 @@ import { noop, get, omit, pick } from 'lodash'; /** * WordPress dependencies */ -import { addFilter, removeAllFilters } from '@wordpress/hooks'; +import { addFilter, removeAllFilters, removeFilter } from '@wordpress/hooks'; import { select } from '@wordpress/data'; import { blockDefault as blockIcon } from '@wordpress/icons'; @@ -836,6 +836,12 @@ describe( 'blocks', () => { } ); } ); test( 'registers block from metadata with translation', () => { + addFilter( + 'i18n.gettext_with_context_test', + 'test/mark-as-translated', + ( value ) => value + ' (translated)' + ); + const Edit = () => 'test'; const block = registerBlockTypeFromMetadata( { @@ -845,11 +851,11 @@ describe( 'blocks', () => { keywords: [ 'i18n', 'metadata' ], styles: [ { - label: 'i18n-metadata', - title: 'I18n Metadata', + name: 'i18n-metadata', + label: 'I18n Metadata', }, ], - textdomain: 'i18n', + textdomain: 'test', icon: 'palmtree', }, { @@ -857,22 +863,27 @@ describe( 'blocks', () => { save: noop, } ); + removeFilter( + 'i18n.gettext_with_context_test', + 'test/mark-as-translated' + ); + expect( block ).toEqual( { name: 'test/block-from-metadata-i18n', - title: 'I18n title from metadata', - description: 'I18n description from metadata', + title: 'I18n title from metadata (translated)', + description: 'I18n description from metadata (translated)', icon: { src: 'palmtree', }, - keywords: [ 'i18n', 'metadata' ], + keywords: [ 'i18n (translated)', 'metadata (translated)' ], attributes: {}, providesContext: {}, usesContext: [], supports: {}, styles: [ { - label: 'i18n-metadata', - title: 'I18n Metadata', + name: 'i18n-metadata', + label: 'I18n Metadata (translated)', }, ], edit: Edit, From f43726209ef79d90e1cf81fc0074621329e0c99f Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Thu, 22 Apr 2021 16:40:34 +0200 Subject: [PATCH 7/8] Docs: Add changelog entry --- bin/plugin/commands/common.js | 1 + packages/blocks/CHANGELOG.md | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/bin/plugin/commands/common.js b/bin/plugin/commands/common.js index 57326be7205bf..4f3e0bb065243 100644 --- a/bin/plugin/commands/common.js +++ b/bin/plugin/commands/common.js @@ -116,6 +116,7 @@ function calculateVersionBumpFromChangelog( if ( lineNormalized.startsWith( '### deprecation' ) || lineNormalized.startsWith( '### enhancement' ) || + lineNormalized.startsWith( '### new api' ) || lineNormalized.startsWith( '### new feature' ) ) { versionBump = 'minor'; diff --git a/packages/blocks/CHANGELOG.md b/packages/blocks/CHANGELOG.md index 8a45220bdf44a..498eb1bd41825 100644 --- a/packages/blocks/CHANGELOG.md +++ b/packages/blocks/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### New API + +- `registerBlockTypeFromMetadata` method can be used to register a block type using the metadata loaded from `block.json` file ([#30293](https://github.com/WordPress/gutenberg/pull/30293)). + ## 8.0.0 (2021-03-17) ### Breaking Change From f371ba88b9e551aac42e75ad290d3419b613797e Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Fri, 23 Apr 2021 09:34:26 +0200 Subject: [PATCH 8/8] Add i18n support for variations read from block metadata --- packages/blocks/src/api/i18n-block.json | 11 ++++-- packages/blocks/src/api/registration.js | 3 +- packages/blocks/src/api/test/registration.js | 40 ++++++++++++++++++-- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/packages/blocks/src/api/i18n-block.json b/packages/blocks/src/api/i18n-block.json index 9b44a6ec078ae..3d31f78592eaa 100644 --- a/packages/blocks/src/api/i18n-block.json +++ b/packages/blocks/src/api/i18n-block.json @@ -1,12 +1,17 @@ { "title": "block title", "description": "block description", - "keywords": [ - "block keyword" - ], + "keywords": [ "block keyword" ], "styles": [ { "label": "block style label" } + ], + "variations": [ + { + "title": "block variation title", + "description": "block variation description", + "keywords": [ "block variation keyword" ] + } ] } diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 7a4e0f901e46f..b1f62fe4ee9cb 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -387,6 +387,7 @@ export function registerBlockTypeFromMetadata( additionalSettings ) { const allowedFields = [ + 'apiVersion', 'title', 'category', 'parent', @@ -399,7 +400,7 @@ export function registerBlockTypeFromMetadata( 'supports', 'styles', 'example', - 'apiVersion', + 'variations', ]; const settings = pick( metadata, allowedFields ); diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index ead44e97c1021..b8d7c9c413754 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -812,6 +812,14 @@ describe( 'blocks', () => { title: 'Block from metadata', category: 'text', icon: 'palmtree', + variations: [ + { + name: 'variation', + title: 'Variation Title', + description: 'Variation description', + keywords: [ 'variation' ], + }, + ], }, { edit: Edit, @@ -831,6 +839,14 @@ describe( 'blocks', () => { usesContext: [], supports: {}, styles: [], + variations: [ + { + name: 'variation', + title: 'Variation Title', + description: 'Variation description', + keywords: [ 'variation' ], + }, + ], edit: Edit, save: noop, } ); @@ -851,8 +867,16 @@ describe( 'blocks', () => { keywords: [ 'i18n', 'metadata' ], styles: [ { - name: 'i18n-metadata', - label: 'I18n Metadata', + name: 'i18n-style', + label: 'I18n Style Label', + }, + ], + variations: [ + { + name: 'i18n-variation', + title: 'I18n Variation Title', + description: 'I18n variation description', + keywords: [ 'variation' ], }, ], textdomain: 'test', @@ -882,8 +906,16 @@ describe( 'blocks', () => { supports: {}, styles: [ { - name: 'i18n-metadata', - label: 'I18n Metadata (translated)', + name: 'i18n-style', + label: 'I18n Style Label (translated)', + }, + ], + variations: [ + { + name: 'i18n-variation', + title: 'I18n Variation Title (translated)', + description: 'I18n variation description (translated)', + keywords: [ 'variation (translated)' ], }, ], edit: Edit,