Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent specific block from removing #16364

Closed
woorise opened this issue Jun 30, 2019 · 10 comments
Closed

Prevent specific block from removing #16364

woorise opened this issue Jun 30, 2019 · 10 comments
Labels
[Feature] Block API API that allows to express the block paradigm. [Type] Enhancement A suggestion for improvement.

Comments

@woorise
Copy link

woorise commented Jun 30, 2019

I'm inserting a block programmatically and I want to prevent users from removing it. Is there any way to disable the "Remove Block" option for a specific block?

@swissspidy swissspidy added [Feature] Block API API that allows to express the block paradigm. Needs Technical Feedback Needs testing from a developer perspective. labels Jul 1, 2019
@noisysocks noisysocks removed the Needs Technical Feedback Needs testing from a developer perspective. label Jul 11, 2019
@noisysocks
Copy link
Member

Hey @woorise! There's no API that lets third parties disable the Remove Block option for a specific block.

There is, however, Templates which lets you specify which blocks should be in a post by default and lock them down so that the user can't add/remove any further blocks.

Does that cover your use case? If not, could you go into some detail on what you're trying to accomplish?

@noisysocks noisysocks added the [Status] Needs More Info Follow-up required in order to be actionable. label Jul 11, 2019
@woorise
Copy link
Author

woorise commented Jul 13, 2019

I tried the Templates and more specifically the PHP locking snippet with the "insert" option to lock a specific block but it doesn't allow me to insert any other block (different type).

Is this the default behavior?

@noisysocks
Copy link
Member

Yes that's right, template locking prevents both insertion and deletion.

I don't think it makes sense to allow one without the other. For example, if a user can insert blocks but not delete them, how can they remove blocks that are accidentally inserted?

Could you go into some detail on what you're trying to accomplish?

@woorise
Copy link
Author

woorise commented Jul 15, 2019

Template locking prevents insertion and deletion for a specific block or for all blocks?

These are my requirements:

  • A specific block will be added programmatically
  • Users will be able to move this block but not remove it
  • Users will be able to insert other blocks (I mean other types of blocks not the one that we have locked)

@noisysocks
Copy link
Member

Thanks for the extra info, @woorise!

Right now a developer can prevent the user from inserting and deleting all blocks using template locking, but it is not possible to disable inserting and deleting a specific block.

Potentially we could do this by adding supports.remove to the block configuration. This would mirror supports.inserter.

@noisysocks noisysocks added [Type] Enhancement A suggestion for improvement. and removed [Status] Needs More Info Follow-up required in order to be actionable. labels Jul 16, 2019
@woorise
Copy link
Author

woorise commented Jul 16, 2019

Great! This would be a highly welcomed addition.

@pwkip
Copy link
Contributor

pwkip commented May 14, 2020

If you want to make sure that you can't remove a specific block (myplugin/important-block), you could do something like this:

const getBlockList = () => wp.data.select( 'core/block-editor' ).getBlocks();
let blockList = getBlockList();
wp.data.subscribe( () => {
  const newBlockList = getBlockList();
  if (
    newBlockList.length < blockList.length
    && blockList.some( block => block.name === 'myplugin/important-block' ) )
    && newBlockList.every( block => block.name !== 'myplugin/important-block' )
  ) {
    wp.data.dispatch( 'core/block-editor' ).resetBlocks( blockList );
  }
  blockList = newBlockList;
} );

This code will make sure that at least one block with the name myplugin/important-block will be present in the editor.

Also check: https://bdwm.be/gutenberg-prevent-specific-block-from-being-removable/

@costasovo
Copy link
Contributor

@pwkip Thanks for the workaround. But still, it would be nice to have some officially supported block setting for that.

Btw I think that in your fix might be an issue with nested blocks. Some block (e.g. core/columns OR core/column) may carry other blocks on innerBlocks property. blockList.some and blockList.every check only the top level so you may miss the myplugin/important-block if it is inside a column or some other block that support nested blocks.

You can use this function for searching for a block.

const findBlock = (blocks, name) => (
  blocks.reduce((result, block) => {
    if (result) {
      return result;
    }
    if (block.name === name) {
      return block;
    }
    if (Array.isArray(block.innerBlocks) && block.innerBlocks.length) {
      return findBlock(block.innerBlocks, name);
    }
    return null;
  }, null)
);

@RajanLamaFuse
Copy link

Hello support team,
I have disabled the block to be removed by using lock but it only remove the button, and specific block can be remove but copy and paste other block and replace it. Do you have any solution? Highly appreciated. Thank you in advance.

@annezazu
Copy link
Contributor

annezazu commented Sep 6, 2024

Hey folks! I'm coming to the party late on this issue but with some good news. A few releases ago the ability to lock blocks was implemented (locking for both removal and movement): https://developer.wordpress.org/block-editor/how-to-guides/curating-the-editor-experience/block-locking/ This is your best bet for implementing this. This allows other blocks to be implemented and, depending on how you set it up, allows for movement of the block even if you can't delete.

I'm going to close this out as a result but, if I've gotten anything wrong here or if anything is missing, please let me know and we can reopen to continue the discussion.

@annezazu annezazu closed this as completed Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants