gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

insertBlock not adding child blocks

Open andreg opened this issue 6 years ago • 2 comments

Hello there,

in our plugin (Grids), we used to perform the following action in order to add inner blocks to our parent block:

wp.data.dispatch( 'core/block-editor' ).insertBlock( newBlock, index, parentBlockId );

However, in the latest RC of WordPress and in the latest distribution of Gutenberg, the above code doesn't seem to produce any outcome (the array of inner blocks is always empty), and as of right now the functionality of our plugin is broken. Is there something I'm missing here? I've tried to retrace the changes made to the insertBlock action, but couldn't determine what the issue is.

Thanks in advance for your help.

andreg avatar Nov 25 '20 10:11 andreg

@andreg It does still seem to work in my testing with a group block:

wp.data.dispatch( 'core/block-editor' ).insertBlock(
    wp.blocks.createBlock( 'core/heading', {content: 'test'} ), 
    0, 
    '3ac86633-bf91-4fd5-a799-edee8c408ce6'
);

If there were an issue, I think there would generally be lots of noticeable errors in the plugin as this action is quite widely used.

Some things to check are whether you have templateLock or allowedBlocks restrictions on your parent, or parent restrictions on your child.

talldan avatar Nov 25 '20 11:11 talldan

Thank you for the help, @talldan! Restrictions were not an issue, I was able to fix my issue by using the wp.data.dispatch( 'core/block-editor' ).replaceInnerBlocks function.

Not sure it's ideal for every use-case, but it's perfect for ours. You may now close this issue ;)

andreg avatar Nov 25 '20 13:11 andreg