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 removing blocks if templateLock is set to insert #8201

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Sometimes the intention might be to lock the template on the UI so that the bloc
*Options:*

- `all` — prevents all operations. It is not possible to insert new blocks, move existing blocks, or delete blocks.
- `insert` — prevents inserting new blocks, but allows moving or removing existing blocks.
- `insert` — prevents inserting or removing blocks, but allows moving existing blocks.

## Existing Post Types

Expand Down
6 changes: 5 additions & 1 deletion editor/components/block-settings-menu/block-remove-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export default compose(
isLocked: some( castArray( clientIds ), ( clientId ) => {
const rootClientId = getBlockRootClientId( clientId );
const templateLock = getTemplateLock( rootClientId );
return templateLock === 'all';

// Prevents deletion if templateLock is "all" or "insert"
// (if a user can't insert blocks they shouldn't be able
// to remove them either)
return templateLock === 'all' || templateLock === 'insert';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: In other places we do !! templateLock (inserting blocks).

I think we should add a similar check to SharedBlockDeleteButton? what do you think? in case of a template containing a shared block.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, on both counts. But I didn't know a template could include a shared block — how does that work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Like, how do you know the name of the block slug to put into the template? Is it just the name you save, or a kebab-cased version of that?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try adding a shared block to a regular post and copy the result (it should be core/block with a ref attribute)

} ),
};
} ),
Expand Down
8 changes: 6 additions & 2 deletions editor/components/inner-blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const ALLOWED_BLOCKS = [ 'core/image', 'core/paragraph' ];
allowedBlocks={ ALLOWED_BLOCKS }
/>
```

The previous code block creates an `InnerBlocks` area where only image and paragraph blocks can be inserted.

Child blocks that have marked themselves as compatible are not excluded from the allowed blocks. Even if `allowedBlocks` doesn't specify a child block, a registered child block will still appear on the inserter for this block.
Expand All @@ -64,6 +65,7 @@ const ALLOWED_BLOCKS = [];
allowedBlocks={ ALLOWED_BLOCKS }
/>
```

The previous code block restricts all blocks, so only child blocks explicitly registered as compatible with this block can be inserted. If no child blocks are available: it will be impossible to insert any inner blocks.

### `template`
Expand All @@ -86,6 +88,7 @@ const TEMPLATE = [ [ 'core/columns', {}, [
template={ TEMPLATE }
/>
```

The previous example creates an InnerBlocks area containing two columns one with an image and the other with a paragraph.

### `templateLock`
Expand All @@ -94,10 +97,11 @@ The previous example creates an InnerBlocks area containing two columns one with
Template locking of `InnerBlocks` is similar to [Custom Post Type templates locking](https://wordpress.org/gutenberg/handbook/templates/#locking).

Template locking allows locking the `InnerBlocks` area for the current template.

*Options:*

- `all` — prevents all operations. It is not possible to insert new blocks. Move existing blocks or delete them.
- `insert` — prevents inserting new blocks, but allows moving or removing existing ones.
- `all` — prevents all operations. It is not possible to insert new blocks, move existing blocks, or delete them.
- `insert` — prevents inserting or removing blocks, but allows moving existing ones.
- `false` — prevents locking from being applied to an `InnerBlocks` area even if a parent block contains locking.

If locking is not set in an `InnerBlocks` area: the locking of the parent `InnerBlocks` area is used.
Expand Down