From f1801d706893303f93e7f4b7cac010fa89955cae Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 17 Oct 2022 10:51:59 +0300 Subject: [PATCH] Allow defining a form-ID --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/form/block.json | 4 +++ packages/block-library/src/form/edit.js | 34 ++++++++++++++++++++-- packages/block-library/src/form/index.php | 5 ++-- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 798241d699c2e5..95a37b58622793 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -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 diff --git a/packages/block-library/src/form/block.json b/packages/block-library/src/form/block.json index 30d509fab6d48a..fe7da5ebe5b368 100644 --- a/packages/block-library/src/form/block.json +++ b/packages/block-library/src/form/block.json @@ -15,6 +15,10 @@ "method": { "type": "string", "default": "post" + }, + "formId": { + "type": "string", + "default": "" } } } diff --git a/packages/block-library/src/form/edit.js b/packages/block-library/src/form/edit.js index d81171d3c7e085..e303cde8690e2d 100644 --- a/packages/block-library/src/form/edit.js +++ b/packages/block-library/src/form/edit.js @@ -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', @@ -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
; + return ( + <> + + + { + setAttributes( { + formId: newVal, + } ); + } } + help={ __( + 'Unique identifier for this form. This value gets sent along with the form submission.' + ) } + /> + + + ; + + ); }; export default Edit; diff --git a/packages/block-library/src/form/index.php b/packages/block-library/src/form/index.php index b2a34188d3455c..d58ebc64624514 100644 --- a/packages/block-library/src/form/index.php +++ b/packages/block-library/src/form/index.php @@ -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( '', + '', ), $content );