Block transforms: convert between your block and core blocks
The “Transform” button in the block toolbar turns a paragraph into a heading, a list into a quote, several images into a gallery, without retyping anything. Custom blocks get the same convenience by declaring transforms in their registration: from lists what can become your block, to lists what your block can become.
Problem: Editors cannot turn an existing paragraph into the custom “Callout” block, or a callout back into a paragraph; they have to delete the block and paste the text again.
Solution: Add a transforms property to registerBlockType() with block transforms to and from core/paragraph, and create the target block with createBlock() from wp.blocks.
The transforms
The example is plain JavaScript for a block registered without a build step (the same object works in a JSX build):
( function ( blocks ) {
var createBlock = blocks.createBlock;
blocks.registerBlockType( 'ha/callout', {
title: 'Callout',
category: 'common',
attributes: {
text: { type: 'string', source: 'html', selector: 'p' }
},
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: function ( attributes ) {
return createBlock( 'ha/callout', { text: attributes.content } );
}
},
{
// several selected paragraphs -> one callout
type: 'block',
blocks: [ 'core/paragraph' ],
isMultiBlock: true,
transform: function ( attributesList ) {
var text = attributesList.map( function ( a ) { return a.content; } ).join( '<br>' );
return createBlock( 'ha/callout', { text: text } );
}
}
],
to: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: function ( attributes ) {
return createBlock( 'core/paragraph', { content: attributes.text } );
}
},
{
type: 'block',
blocks: [ 'core/heading' ],
transform: function ( attributes ) {
return createBlock( 'core/heading', { content: attributes.text, level: 3 } );
}
}
]
},
edit: function () { /* ... */ },
save: function () { /* ... */ }
} );
} )( window.wp.blocks );
A from callback receives the attributes of the source block (for a paragraph: content, align, dropCap…), a to callback receives the attributes of your block. Both return a block object made with createBlock( name, attributes, innerBlocks ), never HTML.
With isMultiBlock: true the transform is offered when several blocks of that type are selected, and the callback gets an array of their attributes.
Other transform types
type: 'shortcode': a[callout]shortcode from the classic editor becomes your block when the post is converted to blocks.type: 'raw': pasted or converted HTML that matches anisMatchfunction becomes your block.
Shortcode and raw transforms are the way to migrate old content: editors click “Convert to blocks” on a classic post and get real blocks instead of shortcodes. A basic block without a build step, which this example extends, is shown in creating your first custom block.
NOTE: Map every attribute you can. A transform that drops data (an alignment, a link, a colour) silently loses it, and the editor notices only after saving. If a value has no equivalent in the target block, it is better not to offer that transform at all.