Gutenberg – How to prevent block from being deleted and reordered/moved

I have a post type which is prepopulated with a Gutenberg block:

function blog_block_template() {
  $post_type_event = get_post_type_object( 'event' );
  $post_type_event->template = array(
    array( 'acf/single-event-meta' ),
  );
}
add_action( 'init', 'blog_block_template' );

This certain block should not be deleted or reordered. I don’t want to use a meta box because the meta box would be rendered below all Gutenberg blocks or on the side which would be confusing for the user.

There’s the supports parameter when registering a Gutenberg block which allows to restrict some functionality but I couldn’t find anything about deleting or reordering.

'supports' => array( 
    'align'           => false, 
    'customClassName' => false,
    'html'            => false,
    'inserter'        => false,
    'multiple'        => false,
    'reusable'        => false,
),

Thanks!

1 Answer
1

You could use a block template.

For any given post type, you can set up a block template which adds blocks to the Editor whenever you create a new post of that type. These templates have an option called template_lock which you can set to all which means that users cannot add, move, or delete any blocks.

Of course you probably want to let people add other blocks – but you can still use a template. You would need to create your own wrapper block, one that holds your event block inside of it – and that also allows InnerBlocks. This way, your wrapper with its event will always be required, but people can still add, edit, move, and delete any other blocks on the page as needed.

Leave a Comment