Skip to content

Instantly share code, notes, and snippets.

@pattyok
Last active May 23, 2023 15:52
Show Gist options
  • Save pattyok/120aa0a88fbeacc27f6c5b3875094e14 to your computer and use it in GitHub Desktop.
Save pattyok/120aa0a88fbeacc27f6c5b3875094e14 to your computer and use it in GitHub Desktop.
WordPress Block Transform from ACF Repeater Block
/** Transform from an ACF block with repeating rows, to a custom block with child blocks
the content field on the ACF Block is a WYSIWIG Editor so we translate it to RAW HTML and pass it as inner blocks to the new accordion panel **/
wp.hooks.addFilter(
'blocks.registerBlockType',
'local27/accordion-transform',
function( settings, name ) {
if ( name === 'acf/accordion' ) {
settings.transforms = {
to: [
{
type: 'block',
blocks: [ 'carkeek-blocks/accordion' ],
transform: ( attributes ) => {
const count = attributes.data.accordion_repeater;
let innerBlocks = [];
for (let x = 0; x < count; x++) {
const content = wp.blocks.rawHandler({
HTML: attributes.data['accordion_repeater_' + x + '_accordion_content'],
});
const blocks = wp.blocks.createBlock( 'carkeek-blocks/accordion-panel', { title: attributes.data['accordion_repeater_' + x + '_accordion_title'], 'inheritedHeaderStyle': 'h3' }, content);
innerBlocks.push(blocks);
}
return wp.blocks.createBlock( 'carkeek-blocks/accordion', {}, innerBlocks );
}
}
]
}
}
return settings;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment