admin管理员组

文章数量:1410724

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!

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!

Share Improve this question edited Jan 28, 2020 at 11:03 user1706680 asked Jan 28, 2020 at 10:26 user1706680user1706680 6943 gold badges13 silver badges29 bronze badges 2
  • Some more context would be helpful in identifying a solution, what's the problem that this solves for you? – Tom J Nowell Commented Jan 28, 2020 at 10:33
  • Thanks for asking. I added some context. I hope that clarifies. – user1706680 Commented Jan 28, 2020 at 10:37
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: Gutenberg – How to prevent block from being deleted and reorderedmoved