Skip to content

Commit

Permalink
Prevent nesting forms
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Nov 1, 2023
1 parent 413ec7e commit b977750
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/block-library/src/form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import metadata from './block.json';
import save from './save';
import variations from './variations';

/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';

const { name } = metadata;

export { metadata, name };
Expand All @@ -18,3 +23,31 @@ export const settings = {
};

export const init = () => initBlock( { name, metadata, settings } );

// Prevent adding forms inside forms.
const DISALLOWED_PARENTS = [ 'core/form' ];
addFilter(
'blockEditor.__unstableCanInsertBlockType',
'removeTemplatePartsFromPostTemplates',
(
canInsert,
blockType,
rootClientId,
{ getBlock, getBlockParentsByBlockName }
) => {
if ( blockType.name !== 'core/form' ) {
return canInsert;
}

for ( const disallowedParentType of DISALLOWED_PARENTS ) {
const hasDisallowedParent =
getBlock( rootClientId )?.name === disallowedParentType ||
getBlockParentsByBlockName( rootClientId, disallowedParentType )
.length;
if ( hasDisallowedParent ) {
return false;
}
}
return true;
}
);

0 comments on commit b977750

Please sign in to comment.