I made a Gutenberg block for my plugin but it doesn't work as expected anymore. It worked with the first version of Gutenberg and I don’t understand what changed since then.
I read some similar questions, but no useful answers: - Wordpress Gutenberg block - Gutenberg Block validation failed - Gutenberg Block Validation Failed ( not expecting children )
My problem in detail:
When I add the block into my content it looks perfect, also if I save the post I see the widget as expected.
The problem is that when I edit an existing article the block is broken: https://ibb.co/6vHRcXC
The code looks good, I can see it in code editor: https://ibb.co/tDMGnPY
Updating the article the widget still works on frontend.
The console shows this warning followed by several errors: https://ibb.co/Gk80skq
It looks like the attribute data-id is missing in generated code, but I don't understand why.
I tried changing this attribute name (in case [data-id] was a reserved word) but nothing changed.
I tried changing the value of this attribute (I need it to be an integer, but I tried adding uppercase, lowercase and underscore prefixes) and nothing changed.
A bit of context: I need a unique ID for every block created with Gutenberg, no repetitions site wide and I need to bring this ID to a JavaScript function on frontend. Please tell me what I am doing wrong and if there is another way to accomplish that.
This is the complete JavaScript source of my Gutenberg block:
(function () {
let iconSvg = wp.element.createElement('svg', {width: 20, height: 20},
wp.element.createElement('path', {d: "M10,0C4.478,0,0,4.478,0,10c0,5.521,4.478,10,10,10c5.521,0,10-4.479,10-10C20,4.478,15.521,0,10,0zM5.039,9.226c-0.786,0-1.425,0.765-1.425,1.705H2.576c0-1.512,1.104-2.742,2.462-2.742s2.463,1.23,2.463,2.742H6.463C6.463,9.991,5.825,9.226,5.039,9.226z M10,18.049c-3.417,0-6.188-2.41-6.188-5.382h12.375C16.188,15.639,13.418,18.049,10,18.049zM16.387,10.931c0-0.94-0.639-1.705-1.426-1.705c-0.785,0-1.424,0.765-1.424,1.705h-1.039c0-1.512,1.105-2.742,2.463-2.742s2.463,1.23,2.463,2.742H16.387z"})
);
wp.blocks.registerBlockType('plugin-name/gutenberg-block', {
title: 'CustomBlockName',
icon: iconSvg,
category: 'widgets',
edit: (props) => {
// @TODO: we need a unique ID for each block!
this.myId = props.attributes.itemId || Math.floor(Math.random() * 8388607); /// Medium int max value is 8388607;
props.setAttributes(
{
itemId: myId
}
);
return wp.element.createElement(
'div',
{},
[
wp.element.createElement(
'div',
{},
[
wp.element.createElement(
'img',
{
src: PluginNameGutenbergBlock.preview_image
}
)
]
)
]
);
},
save: (props) => {
let dataId = props.attributes.itemId;
return wp.element.createElement(
'div',
{},
[
wp.element.createElement(
'div',
{
class: 'plugin-name-outer'
},
[
wp.element.createElement(
'div',
{
'class': 'plugin-name-container-async gutenberg-block',
'data-id': dataId,
'data-type': PluginNameGutenbergBlock.item_type
},
wp.element.createElement(
'div',
{
'class': 'plugin-name-' + PluginNameGutenbergBlock.use_template
}, {},
[
wp.element.createElement(
'img',
{
src: PluginNameGutenbergBlock.loader_url
}
)
]
)
)
]
)
]
);
}
});
}());