Block API: Add special case for metadata attribute in 'isUnmodifiedBlock'#70903
Block API: Add special case for metadata attribute in 'isUnmodifiedBlock'#70903
Conversation
11fc18d to
eb9ecaa
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: +12 B (0%) Total Size: 1.89 MB
ℹ️ View Unchanged
|
| // It can include block bindings that serve as a source of content, | ||
| // without directly modifying content attributes. | ||
| if ( role === 'content' && key === 'metadata' ) { | ||
| return true; |
There was a problem hiding this comment.
We can be more granular here, but then it would mean we have to keep track of every new metadata sub-attribute.
return (
Object.keys( block.attributes[ key ].bindings ?? {} )
.length > 0
);|
cc @megane9988 |
t-hamano
left a comment
There was a problem hiding this comment.
Thanks for the PR!
Since the custom name of a block is stored as a metadata attribute as well, if only the name is changed, it will no longer be possible to delete the block with the backspace key, even if the content is empty. Is this behavior acceptable?
I tested it with the following code.
PHP:
add_action( 'init', function() {
register_meta(
'post',
'text_short',
array(
'label' => __( 'Genre' ),
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'sanitize_callback' => 'wp_strip_all_tags',
'default' => 'Some text for a Paragraph',
)
);
} );Blocks:
<!-- wp:image -->
<figure class="wp-block-image"><img alt=""/></figure>
<!-- /wp:image -->
<!-- wp:paragraph {"metadata":{"name":"My Paragraph"}} -->
<p></p>
<!-- /wp:paragraph -->|
As I noted above, we can do a more granular check, but that implies hidden knowledge for devs when they implement something like block bindings to remember and update this condition. I would like to avoid that. I think the |
t-hamano
left a comment
There was a problem hiding this comment.
Okay, so let's go ahead with this approach 👍
|
Thanks, @t-hamano 🙇 Let's leave it over the weekend, just in case others have different opinions :) |
|
Hi @Mamaduka ! Thanks for the PR. As I understood, this will set the block as "modified" if the user makes any change in the metadata attribute, like block bindings, right? |
That's correct. |
Has been tested with patterns revisions and overrides? The PR looks great by the way! |
I think e2e tests cover that. Is there anything particular we should test? I've also added a separate unit test for the utility method; it's a pure function, so there should be no side effects. |
E2E tests should be enough. But I would also try to create a pattern override or and check the revisions. |
|
Thanks for the reviews, folks! |
…ock' (WordPress#70903) Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
What?
This is a follow-up to #70764.
Related #70897.
PR adds a special case for the
metadataattribute, making sure block modification is correctly checked when attributes are bound to external sources.Why?
While the
metadataattribute can't have a role itself, it can contain data that could serve as content. Counting any metadata modifications as a change will prevent accidental "content" loss.Testing Instructions
Test meta source
Testing Instructions for Keyboard
Same.
Screenshots or screencast
CleanShot.2025-07-25.at.08.51.41.mp4