Skip to content

Commit

Permalink
Add initial PHP implementation & filters for form action & method
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Sep 21, 2022
1 parent 08e2457 commit dd9abd9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function gutenberg_reregister_core_block_types() {
'comments-title.php' => 'core/comments-title',
'comments.php' => 'core/comments',
'file.php' => 'core/file',
'form.php' => 'core/form',
'home-link.php' => 'core/home-link',
'image.php' => 'core/image',
'gallery.php' => 'core/gallery',
Expand Down
12 changes: 11 additions & 1 deletion packages/block-library/src/form/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@
"category": "common",
"description": "A form.",
"keywords": [ "container", "wrapper", "row", "section" ],
"textdomain": "default"
"textdomain": "default",
"attributes": {
"action": {
"type": "string",
"default": "#"
},
"method": {
"type": "string",
"default": "get"
}
}
}
72 changes: 72 additions & 0 deletions packages/block-library/src/form/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Server-side rendering of the `core/form` block.
*
* @package WordPress
*/

/**
* Renders the `core/form` block on server.
*
* @param array $attributes The block attributes.
* @param string $content The saved content.
* @param WP_Block $block The parsed block.
*
* @return string The content of the block being rendered.
*/
function render_block_core_form( $attributes, $content, $block ) {

// Build the form action attribute.
$action = empty( $attributes['action'] ) ? '' : $attributes['action'];
$action = empty( $action ) ? '#' : $action;

/**
* Filters the form action attribute.
*
* @param string $action The form action attribute.
* @param array $attributes The block attributes.
* @param string $content The saved content.
* @param WP_Block $block The parsed block.
*
* @return string The form action attribute.
*/
$action = apply_filters( 'block_form', $action, $attributes, $content, $block );

// Build the form method attribute.
$method = empty( $attributes['method'] ) ? '' : $attributes['method'];
$method = empty( $method ) || ( 'get' !== strtolower( $method ) && 'post' !== strtolower( $method ) ) ? 'get' : $method;
/**
* Filters the form method attribute.
*
* @param string $method The form method attribute.
* @param array $attributes The block attributes.
* @param string $content The saved content.
* @param WP_Block $block The parsed block.
*
* @return string The form method attribute.
*/
$method = apply_filters( 'block_form_method', $method, $attributes, $content, $block );

return str_replace(
'<form ',
sprintf(
'<form action="%1$s" method="%2$s" ',
esc_attr( $action ),
esc_attr( $method )
),
$content
);
}

/**
* Registers the `core/form` block on server.
*/
function register_block_core_form() {
register_block_type_from_metadata(
__DIR__ . '/form',
array(
'render_callback' => 'render_block_core_form',
)
);
}
add_action( 'init', 'register_block_core_form' );
1 change: 1 addition & 0 deletions packages/block-library/src/input-field/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
PanelBody,
CustomSelectControl,
TextControl,
CheckboxControl,
} from '@wordpress/components';
import { useRef } from '@wordpress/element';
Expand Down

0 comments on commit dd9abd9

Please sign in to comment.