The Block Utils API provides functions specific to blocks.
The findBlockByName
function allows for finding the first block in the editor for the given block name.
Pass a block name to findBlockByName
, and then use the returned block, if any.
const imageBlock = findBlockByName( 'core/image' );
if ( ! imageBlock ) {
return;
}
// Use imageBlock...
// findBlockByName :: ( blockName: string ) => ?object
const block = findBlockByName( blockName );
The name of the block.
Type | Required | Default |
---|---|---|
string |
yes | undefined |
The findBlockByName
function returns the first block in the editor for the given block name.
If no block was found, the function returns undefined
.
const block = findBlockByName( blockName );
The findInvalidBlock
function allows for finding the first block that is considered "invalid", given a validation function and a list of blocks to test.
Pass an array of blocks and a validation function taking a single block as argument to findInvalidBlock
, and then use the returned block, if any.
const customBlock = findInvalidBlock( blocks, ( { name } ) => /^core\//.test( name ) );
if ( ! customBlock ) {
return;
}
// Use customBlock...
// findInvalidBlock :: ( blocks: object[], isValid: Function ) => ?object
const invalidBlock = findInvalidBlock( blocks, isValid );
An array of blocks.
Type | Required | Default |
---|---|---|
object[] |
yes | undefined |
A validation function, taking a single block object as only argument, and returning a Boolean value.
Type | Required | Default |
---|---|---|
Function |
yes | undefined |
The findInvalidBlock
function returns the first block from the given blocks that is "invalid" with respect to the passed validation function.
If no block was found, the function returns undefined
.
const invalidBlock = findInvalidBlock( blocks, isValid );
The findInvalidBlocks
function allows for finding all blocks that are considered "invalid", given a validation function and a list of blocks to test.
Pass an array of blocks and a validation function taking a single block as argument to findInvalidBlocks
, and then use the returned blocks, if any.
const customBlocks = findInvalidBlocks( blocks, ( { name } ) => /^core\//.test( name ) );
if ( ! customBlocks.length ) {
return;
}
// Use customBlocks...
// findInvalidBlocks :: ( blocks: object[], isValid: Function ) => object[]
const invalidBlocks = findInvalidBlocks( blocks, isValid );
An array of blocks.
Type | Required | Default |
---|---|---|
object[] |
yes | undefined |
A validation function, taking a single block object as only argument, and returning a Boolean value.
Type | Required | Default |
---|---|---|
Function |
yes | undefined |
The findInvalidBlocks
function returns an array with all blocks from the given ones that are "invalid" with respect to the passed validation function.
If no block was found, the function returns an empty array.
const invalidBlocks = findInvalidBlocks( blocks, isValid );
The findValidBlock
function allows for finding the first block that is considered "valid", given a validation function and a list of blocks to test.
Pass an array of blocks and a validation function taking a single block as argument to findValidBlock
, and then use the returned block, if any.
const coreBlock = findValidBlock( blocks, ( { name } ) => /^core\//.test( name ) );
if ( ! coreBlock ) {
return;
}
// Use coreBlock...
// findValidBlock :: ( blocks: object[], isValid: Function ) => ?object
const validBlock = findValidBlock( blocks, isValid );
An array of blocks.
Type | Required | Default |
---|---|---|
object[] |
yes | undefined |
A validation function, taking a single block object as only argument, and returning a Boolean value.
Type | Required | Default |
---|---|---|
Function |
yes | undefined |
The findValidBlock
function returns the first block from the given blocks that is "valid" with respect to the passed validation function.
If no block was found, the function returns undefined
.
const validBlock = findValidBlock( blocks, isValid );
The findValidBlocks
function allows for finding all blocks that are considered "valid", given a validation function and a list of blocks to test.
Pass an array of blocks and a validation function taking a single block as argument to findValidBlocks
, and then use the returned blocks, if any.
const coreBlocks = findValidBlocks( blocks, ( { name } ) => /^core\//.test( name ) );
if ( ! coreBlocks.length ) {
return;
}
// Use coreBlocks...
// findValidBlocks :: ( blocks: object[], isValid: Function ) => object[]
const validBlocks = findValidBlocks( blocks, isValid );
An array of blocks.
Type | Required | Default |
---|---|---|
object[] |
yes | undefined |
A validation function, taking a single block object as only argument, and returning a Boolean value.
Type | Required | Default |
---|---|---|
Function |
yes | undefined |
The findValidBlocks
function returns an array with all blocks from the given ones that are "valid" with respect to the passed validation function.
If no block was found, the function returns an empty array.
const validBlocks = findValidBlocks( blocks, isValid );
The Block Utils require the following dependencies, which are expected to be available:
@wordpress/data