Opened 3 months ago
Last modified 6 weeks ago
#61422 new feature request
registerBlockVariation not holding the blockName param
Reported by: | heymehedi | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Editor | Keywords: | |
Focuses: | javascript | Cc: |
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)
#2
@
3 months 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
@
6 weeks 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
.
Could you provide the code example of the intended usage to better illustrate your use case?