Block Context in editor.BlockEdit filter

Viewed 443

I’m trying to extend a core gutenberg block so that it has a couple of additional options only when it’s contained in a certain custom made block. I thought block context would make this a simple task — set the container block to providesContext and use the blocks.registerBlockType filter to add usesContext to the core block. In the editor.BlockEdit filter, I thought I’d be able to get context from the block’s props. However, context is not one of the props. To make sure I wasn’t doing anything wrong around context, I used the editor.BlockEdit filter on one of my custom blocks that I knew was receiving block context correctly. It doesn’t have context as one of its props either.

Does anyone know of a way to get block context into the editor.BlockEdit filter?

For reference, here is a snippet of the code so far:

    title: 'Card',
    icon: 'images-alt2',
    category: 'utdesign_system',
    description: '',
    attributes: {
        blockName: {
            type: 'string',
            default: 'card',
        }
    },
    providesContext: {
        'card/blockName': 'blockName',
    },
    edit,
    save,
} );

function addImageContext( settings ) {
    if( settings.name == 'core/image' ){
        settings.usesContext = Object.assign( ['card/blockName'] );
    }
    return settings;
}

addFilter(
    'blocks.registerBlockType',
    'gutenstrap/image-context',
    addImageContext
);

const addImageControls =  createHigherOrderComponent( ( BlockEdit ) => {
    return ( props ) => {
        
        const {
            name,
            attributes,
            setAttributes,
            context,
            isSelected,
        } = props;
        //do stuff with context
    };
}, "addImageControls" );

addFilter(
    'editor.BlockEdit',
    'gutenstrap/image-context',
    addImageControls
);```
1 Answers
Related