-
Hi, I was looking for a way to wrap the inner blocks of the core/details block in a container-div to simplify animated toggling of the initially hidden content. While I assume this is also possible using the render-block filter to change the markup only in the front-end, after a lot of googling I found this older tutorial ( https://jschof.com/gutenberg-blocks/using-gutenberg-filters-to-extend-blocks/ ) that explains how one can add a wrapper around the edit and save functions using the block-filter blocks.registerBlockType. However, this also implies that, using this filter, the original edit and save functions of any registered block can also be completely replaced by new edit and save functions, in this case to add the additional div around the innerBlocks component. So I copied the relevant code from the Details block's DetailsEdit and save functions into two new functions called NewDetailsEdit and NewDetailsSave, and within those I added an additional wrapper div around the innerBlocks component. function NewDetailsEdit( { attributes, setAttributes, clientId } ) {
(relevant parts only)
return (
<details
{ ...innerBlocksProps }
open={ hasSelection || showContent }
>
<summary onClick={ ( event ) => event.preventDefault() }>
<RichText
paramters omitted
/>
</summary>
<div class="this-is-my-new-containter-class">
{ innerBlocksProps.children }
</div>
</details>
);
} function NewDetailsSave( { attributes } ) {
(relevant parts only)
return (
<details { ...blockProps } open={ showContent }>
<summary>
<RichText.Content value={ summary } />
</summary>
<div class="this-is-my-new-containter-class">
<InnerBlocks.Content />
</div>
</details>
);
} After this I call the filter, adapted from the tutorial linked to above, and instead of the default edit and save functions call the new components that have the additional div. const { addFilter } = wp.hooks;
/**
* When this function gets run by the addfilter hook below, the filter passes it the block settings or config file
* @param settings
*/
const filterBlocks = ( settings ) => {
if ( settings.name !== 'core/details' ) {
return settings;
}
const newSettings = {
...settings,
edit( props ) {
return (
<NewDetailsEdit
attributes = {props.attributes}
setAttributes = {props.setAttributes}
clientId = {props.clientId}
/>
);
},
save( props ) {
return (
<NewDetailsSave
attributes = {props.attributes}
/>
);
},
};
return newSettings;
};
addFilter(
'blocks.registerBlockType', // hook name
'example/filter-blocks', // arbitrary
filterBlocks // function to run
); This works... this way I get a core/details block that has an additional wrapper div both in the editor and the front-end. My question is: is this how this kind of markup extension for core blocks, or potentially core-block-variations is supposed to be done? I understand that changing the markup will lead to validation errors for extisting blocks, and that it may be simpler to just fork the details block into a custom block. But still, this seems this is working method to achieve core block markup changes both in the editor and the front end. Is this how it's supposed to be done? Or maybe there's an even better method I'm unaware of? Thanks for your input! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
I think a much simpler approach would be using a block variation to preload a Details block with a group block inside of it:
Reference: https://developer.wordpress.org/block-editor/reference-guides/block-api/block-variations/ |
Beta Was this translation helpful? Give feedback.
I think a much simpler approach would be using a block variation to preload a Details block with a group block inside of it:
Reference: https://developer.wordpress.org/block-editor/reference-guides/block-api/block-variations/