Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#61422 new feature request

registerBlockVariation not holding the blockName param

Reported by: heymehedi Owned by:
Priority: normal Milestone: Awaiting Review
Component: Editor Version:
Severity: normal Keywords:
Cc: Focuses: javascript

Description

After adding a variation of a core block like core/button or any other, It can't access the blockName of the newly created variation. I hope this is important to have.

Change History (3)

#1 @gziolo
2 years ago

  • Component General → Editor

Could you provide the code example of the intended usage to better illustrate your use case?

#2 @heymehedi
2 years ago

Hi @gziolo ,

Here you go:

I have created a variation using a hook.

wp.blocks.registerBlockVariation( 'core/button', {
	title: __( 'Custom Button Block' ),
    name: 'custom-button-block',
    attributes: {
        textColor: 'vivid-red',
    },
} );

and, then I am trying to do something if the name is 'custom-button-block'

const addButtonBlockCustomAttributes = (settings, name) => {
    if (name !== 'custom-button-block') {
        return settings;
    }

	console.log(name);

	// My custom code goes here

    return settings;
};

addFilter(
    'blocks.registerBlockType',
    'gutenberg-button-block-variation/extend-button-block',
    addButtonBlockCustomAttributes
);

#3 @gziolo
2 years ago

I see now what the issue is, when using the name param in the filter, you should check against the block name for which you register the variation: core/button.

Then sometimes you need to detect the variation with some custom code. @ockham started looking into offering a helper function to use on the server get_active_block_variation in ​https://github.com/WordPress/wordpress-develop/pull/6602, but I'm not entirely sure if that will work in all cases. For the client, something similar should be available called getActiveBlockVariation.

In the example you shared, you probably could iterate over settings.variations and change some settings there when the name is equal to custom-button-block.

Last edited 2 years ago by gziolo (previous) (diff)
Note: See TracTickets for help on using tickets.