Blocks don’t work when custom attribute is added
-
It is possible to add custom attributes (for example for permissions) to all blocks (including custom blocks). See: https://jeffreycarandang.com/extending-gutenberg-core-blocks-with-custom-attributes-and-controls/
If this is done – the genesis-custom-blocks don’t work. This is because ServerSideRender renderer on the backend crashes if additional attributes are passed. Ideally it should should not crash but it does (tested in wordpress 5.6). This issue can be fixed by simply filtering the attributes before passing them as props into ServerSideRender called in the edit function of the custom block. Something like this:
`// Filter attributes which are not block attributes
let filteredAttributes = [];
fields.forEach(fieldDef => {
const attributeName = fieldDef[‘name’];
const val = props.attributes[attributeName];
if (val !== null) {
filteredAttributes[attributeName] = val;
}
});return el(‘div’, { …blockProps, ‘className’: className },
el(ServerSideRender, {
block: ‘pbt/’ + blockName,
attributes: filteredAttributes
})
);
The topic ‘Blocks don’t work when custom attribute is added’ is closed to new replies.