Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using groups and columns inside the experimental form block #55758

Merged
merged 3 commits into from Nov 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/block-library/src/form/index.js
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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do template parts and post templates have to do with this? :)

Copy link
Contributor

@t-hamano t-hamano Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, this namespace would need to be changed to something like removeFormFromInserter 😅

Copy link
Member

@gziolo gziolo Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, something like core/block-library/preventInsertingFormIntoAnotherForm would fit better. I don't think we have ever established a good pattern for naming filters.

(
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;
}
);