最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom Gutenberg Block: Excluding an allowed block from InnerBlock's templateLock={'all'}

programmeradmin4浏览0评论

The title pretty much says it all. I am working on a custom Gutenberg block that utilizes the InnerBlocks component and I have defined some allowed and default blocks. I also have templateLock set to all so the user cannot add, remove or move blocks around. However, this settings is affecting the core/buttons block and your ability to add a new button (since this block made up of individual core/button blocks).

Is there any way around this? Is there a way to specifically exclude the core/buttons blocks from the templateLock setting?

Below you will find my edit function for the block.

edit: function (props) {
    const { className, clientId } = props;

    /**
     * Add Item
     */
    const onRemoveItem = () => {
        setAttributes({ items: parseInt(`${items}`) - 1 })
        removeBlock(clientId)
    }

    return (
        <Fragment>
            <div className='listicles-innerblocks' >
                <InnerBlocks
                    template={[
                        ['core/cover'],
                        ['custom/listdt'],
                        ['core/buttons'],
                        ['custom/listdd'],
                    ]}
                    allowedBlocks={[
                        ['core/cover'],
                        ['custom/listdt'],
                        ['core/buttons'],
                        ['custom/listdd']
                    ]}
                    templateLock={'all'}
                />
            </div>
        </Fragment>
    );
},

The title pretty much says it all. I am working on a custom Gutenberg block that utilizes the InnerBlocks component and I have defined some allowed and default blocks. I also have templateLock set to all so the user cannot add, remove or move blocks around. However, this settings is affecting the core/buttons block and your ability to add a new button (since this block made up of individual core/button blocks).

Is there any way around this? Is there a way to specifically exclude the core/buttons blocks from the templateLock setting?

Below you will find my edit function for the block.

edit: function (props) {
    const { className, clientId } = props;

    /**
     * Add Item
     */
    const onRemoveItem = () => {
        setAttributes({ items: parseInt(`${items}`) - 1 })
        removeBlock(clientId)
    }

    return (
        <Fragment>
            <div className='listicles-innerblocks' >
                <InnerBlocks
                    template={[
                        ['core/cover'],
                        ['custom/listdt'],
                        ['core/buttons'],
                        ['custom/listdd'],
                    ]}
                    allowedBlocks={[
                        ['core/cover'],
                        ['custom/listdt'],
                        ['core/buttons'],
                        ['custom/listdd']
                    ]}
                    templateLock={'all'}
                />
            </div>
        </Fragment>
    );
},
Share Improve this question asked Mar 16, 2022 at 14:21 Ebonie ButlerEbonie Butler 311 bronze badge 1
  • Did you ever find a solution to this? – Shoelaced Commented Feb 7, 2024 at 15:49
Add a comment  | 

1 Answer 1

Reset to default 3

core/buttons is a container block, it's not the actual buttons. Instead, core/buttons contains core/button blocks. The same way that a core/columns block contians core/column blocks.

You need to add core/button to your allowed blocks list.

发布评论

评论列表(0)

  1. 暂无评论