Skip to content

Commit

Permalink
Allow defining a form-ID
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Oct 17, 2022
1 parent c402479 commit f1801d7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ A form. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/blo
- **Name:** core/form
- **Category:** common
- **Supports:**
- **Attributes:** action, method
- **Attributes:** action, formId, method

## Classic

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/form/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"method": {
"type": "string",
"default": "post"
},
"formId": {
"type": "string",
"default": ""
}
}
}
34 changes: 31 additions & 3 deletions packages/block-library/src/form/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* WordPress dependencies
*/
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import {
useBlockProps,
useInnerBlocksProps,
InspectorControls,
} from '@wordpress/block-editor';
import { PanelBody, TextControl } from '@wordpress/components';

const ALLOWED_BLOCKS = [
'core/paragraph',
Expand All @@ -11,13 +17,35 @@ const ALLOWED_BLOCKS = [
'core/group',
];

const Edit = () => {
const Edit = ( { attributes, setAttributes } ) => {
const { formId } = attributes;
const blockProps = useBlockProps();

const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
} );

return <form { ...innerBlocksProps } />;
return (
<>
<InspectorControls>
<PanelBody title={ __( 'Form settings' ) }>
<TextControl
autoComplete="off"
label={ __( 'Form ID' ) }
value={ formId }
onChange={ ( newVal ) => {
setAttributes( {
formId: newVal,
} );
} }
help={ __(
'Unique identifier for this form. This value gets sent along with the form submission.'
) }
/>
</PanelBody>
</InspectorControls>
<form { ...innerBlocksProps } />;
</>
);
};
export default Edit;
5 changes: 3 additions & 2 deletions packages/block-library/src/form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ function render_block_core_form( $attributes, $content, $block ) {
*/
$method = apply_filters( 'block_form_method', $method, $attributes, $content, $block );

$form_hash = md5( json_encode( array( $attributes, $block ) ) );
$form_id = empty( $attributes['formId'] ) ? md5( json_encode( array( $attributes, $block ) ) ) : $attributes['formId'];

return str_replace(
array(
'<form ',
Expand All @@ -59,7 +60,7 @@ function render_block_core_form( $attributes, $content, $block ) {
esc_attr( $action ),
esc_attr( $method )
),
'<input type="hidden" name="block-form-hash" value="' . $form_hash . '" /><form>',
'<input type="hidden" name="block-form-id" value="' . esc_attr( $form_id ) . '" /><form>',
),
$content
);
Expand Down

0 comments on commit f1801d7

Please sign in to comment.