Opened 2 years ago
Last modified 4 weeks ago
#61406 new defect (bug)
Heading block attribute type issue
| Reported by: | metaphorcreations | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Posts, Post Types | Version: | 6.5.4 |
| Severity: | normal | Keywords: | needs-test-info has-patch has-unit-tests |
| Cc: | Focuses: |
Description
The Heading block is still using rich-text as the type for the content attribute and I am seeing the following warning:
Notice: Function rest_validate_value_from_schema was called incorrectly. The "type" schema keyword for content can only be one of the built-in types: array, object, string, number, integer, boolean, and null.
Change History (11)
This ticket was mentioned in Slack in #core-test by mai21. View the logs.
2 years ago
#3
@
2 years ago
@mai21 I have a custom plugin that works with Gravity Forms and uses blocks to design email templates. This error occurs When a form is submitted and the blocks are rendered for the notifications. I haven't run into the issue when just using blocks on pages/posts, etc... So, it's quite a few steps to replicate.
But, I do know that many core blocks that WordPress loads from the wp-includes/blocks/blocks-json.php file contain "rich-text" types, which is causing the issue for my plugin.
Is there a reason why core block attributes are still using the "rich-text" type when it's not supported anymore?
The type field MUST be one of the following: null boolean object array string integer number (same as integer)
#4
@
2 years ago
@mai21 I created a short video showing the issue: https://www.loom.com/share/90390cf3151e40e6b20f67018955ea48?sid=6e592b3e-3eaf-411c-b387-d58e41fb34a3
#6
@
12 months ago
Hi, @metaphorcreations
Can you create an issue on the Gutenberg repo with reproduction steps? Link: https://github.com/WordPress/gutenberg/issues/new
The rich-text is a valid block attribute type.
#7
@
10 months ago
We are getting this issue in unit tests for RSS feeds. I believe this problem appeared after the WordPress 6.8 update.
When we include the markup of an image block in a test post, example below, the test fails, and we get the notice:
Notice: Function rest_validate_value_from_schema was called <strong>incorrectly</strong>. The "type" schema keyword for caption can only be one of the built-in types: array, object, string, number, integer, boolean, and null. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 5.5.0.) in /tmp/wordpress/wp-includes/functions.php on line 6114
<!-- wp:image {"url":"http://example.org/wp-content/uploads/2022/03/55038451-0-image-a-2_1646636766274.jpg?quality=90\u0026strip=all","caption":"Lorem ipsum","id":10853156,"sizeSlug":"full","linkDestination":"none"} -->
It looks like the following call stack is being run:
\do_blocks -> \render_block -> \WP_Block::render -> \WP_Block::__get -> \WP_Block_Type::prepare_attributes_for_render -> \rest_validate_value_from_schema
When rest_validate_value_from_schema gets to the caption, the parameters are:
Value: Lorem ipsum
Schema: Array
(
[type] => rich-text
[source] => rich-text
[selector] => figcaption
[role] => content
)
This is validating the post content against the data types defined in the block.json file for the core/image block. There, rich-text is an acceptable type, as @Mamaduka says. But rest_validate_value_from_schema expects string, and complains.
To me, this doesn't look like a Gutenberg bug but an issue in the PHP.
It does seem weird that we validate block attributes against the types expected in a REST schema – the context is different. But apparently \WP_Block_Type::prepare_attributes_for_render has been calling rest_validate_value_from_schema since before WP 5.6.
#8
@
10 months ago
The rich-text type was added to the core image block.json in commit https://github.com/WordPress/WordPress/commit/1de40f71be71a2ae45fb615e06a2279b8ad363a0, which brought in Gutenberg 16.7 RC2. A bunch of other blocks also now have rich-text as the data type for attributes like caption.
The docs don't list rich-text as an allowed data type for block attributes: https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#type-validation. rest_validate_value_from_schema follows this list. It validates attributes against this array.
$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
The caption attribute of images is now rich-text, so blocks with this attribute fail the validation.
Maybe we need to stop using rest_validate_value_from_schema for non-REST contexts, or add a filter hook to it which allows developers to adjust the allowed types when needed.
#9
@
10 months ago
I have created https://github.com/WordPress/gutenberg/issues/72180 for this. It seems like an issue between WordPress and Gutenberg. Gutenberg has defined data types for image captions which WordPress block rendering doesn't expect.
This ticket was mentioned in PR #11985 on WordPress/wordpress-develop by @tusharbharti.
8 weeks ago
#10
- Keywords has-patch has-unit-tests added
Trac ticket: https://core.trac.wordpress.org/ticket/61406
Normalize
rich-textblock attribute type tostringinWP_Block_Type::set_props()
Block attributes with type: "rich-text" (used in block.json for the editor) are not valid JSON Schema types. When WP_Block_Type::prepare_attributes_for_render() calls rest_validate_value_from_schema(), the unrecognised rich-text type triggers a _doing_it_wrong() notice:
The "type" schema keyword for content can only be one of the built-in types: array, object, string, number, integer, boolean, and null.
This affects all core blocks that define attributes as rich-text: paragraph, heading, list-item, image (caption), embed (caption), quote, pullquote, verse, code, preformatted, table, gallery, button, file, video, audio, details, and accordion-heading.
Changes in this PR:
src/wp-includes/class-wp-block-type.php— Inset_props(), normaliserich-texttostringat block registration time. This runs once (not per-render) and occurs before theregister_block_type_argsfilter, so plugins can still override if needed.tests/phpunit/tests/blocks/wpBlockType.php— Added arichtextattribute case totest_prepare_attributes()to verify thatrich-texttyped attributes pass validation without triggering_doing_it_wrong.
Related: Gutenberg issue https://github.com/WordPress/gutenberg/issues/72180
This ticket was mentioned in PR #12333 on WordPress/wordpress-develop by @scruffian.
4 weeks ago
#11
Maps rich-text block attribute schemas to string only for render-time REST validation, so WP_Block_Type::prepare_attributes_for_render() can validate those attributes without mutating registered block metadata.
This keeps non-string rich-text values rejected through the existing string validation path and preserves the registered rich-text schema after validation.
## Use of AI Tools
AI assistance: Yes
Tool(s): OpenAI Codex
Model(s): GPT-5
Used for: Implementing the patch, adding focused PHPUnit coverage, running validation commands, and preparing this PR.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
@metaphorcreations Thanks for reporting the issue. Can you please provide reproducible steps?
Note: Using https://wordpress.org/plugins/test-reports/ can help to add the needed info. Thanks