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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ The basic building block for forms. ([Source](https://github.com/WordPress/guten
- **Name:** core/form-input
- **Experimental:** true
- **Category:** common
- **Parent:** core/form
- **Supports:** anchor, spacing (margin), ~~reusable~~
- **Attributes:** inlineLabel, label, name, placeholder, required, type, value, visibilityPermissions

Expand All @@ -305,7 +304,6 @@ Provide a notification message after the form has been submitted. ([Source](http
- **Name:** core/form-submission-notification
- **Experimental:** true
- **Category:** common
- **Parent:** core/form
- **Supports:**
- **Attributes:** type

Expand All @@ -316,7 +314,6 @@ A submission button for forms. ([Source](https://github.com/WordPress/gutenberg/
- **Name:** core/form-submit-button
- **Experimental:** true
- **Category:** common
- **Parent:** core/form
- **Supports:**
- **Attributes:**

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/form-input/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "core/form-input",
"title": "Input Field",
"category": "common",
"parent": [ "core/form" ],
"ancestor": [ "core/form" ],
"description": "The basic building block for forms.",
"keywords": [ "input", "form" ],
"textdomain": "default",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "core/form-submission-notification",
"title": "Form Submission Notification",
"category": "common",
"parent": [ "core/form" ],
"ancestor": [ "core/form" ],
"description": "Provide a notification message after the form has been submitted.",
"keywords": [ "form", "feedback", "notification", "message" ],
"textdomain": "default",
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/form-submit-button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"title": "Form Submit Button",
"category": "common",
"icon": "button",
"parent": [ "core/form" ],
"ancestor": [ "core/form" ],
"description": "A submission button for forms.",
"keywords": [ "submit", "button", "form" ],
"textdomain": "default",
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/form/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const ALLOWED_BLOCKS = [
'core/form-input',
'core/form-submit-button',
'core/form-submission-notification',
'core/group',
'core/columns',
];

const TEMPLATE = [
Expand Down
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',
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;
}
);
Loading