-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Blocks: Introduce format in attribute definitions, use for Block Bindings
#72716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
DiscussionI'd like to discuss if this could still be considered for WP 6.9. I realize that introducing an addition to
|
|
Size Change: +23 B (0%) Total Size: 2.58 MB
ℹ️ View Unchanged
|
|
I see you found the reference docs for REST API schema. It’s exactly what I had envisioned and also aligns with what @chriszarate shared in his feedback. I would love to see this exact bug fix included in WordPress 6.9 as it’s the most elegant fix possible and at the same time perfectly scalable solution for the future. Newer versions of JSON schema spec added more possible options, but even what we have in place covers the most important string values that have expected structure: date, email, url, color. If necessary, in the future we could mix it with other concepts from the schema like patterns, and build validation in UI on top of it. |
|
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. |
|
Thanks a lot @gziolo! Really appreciate your feedback ❤️ I'll open this PR for wider review, including by Editor Tech Leads for 6.9 and other interested parties. Y'all -- would you be okay with getting this into 6.9? I realize it's late in the cycle, but see @gziolo's points:
|
|
Flaky tests detected in a11a96d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/18868605397
|
|
It's by far the cleanest way to solve incompatibilities when your attribute type is a It's somewhat risky to edit block schema in a Beta, but is not the first time it happens, for the Interactivity API we did the same for block supports. |
|
I like this a lot! Solves several similar headaches I've had before trying to infer the format of a string in block extensions. |
|
The attributes are (or should be) the exact same things as the "fields" API in dataviews. I know we're also looking at introducing "format" for dataviews. So let's make sure this is the exact same API in both before moving forward. cc @oandregal |
The 2019-09 version seems to support those, though: https://json-schema.org/draft/2019-09/draft-handrews-json-schema-validation-02#rfc.section.7.3.1 |
Ah, I only just realized that there are two parts of the JSON Schema spec: Core and Validation. So the
2019-09 Core has those keywords: https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#logic |
Reading that comment, it seems to me that the suggested change was rejected to avoid a collision with a future migration to a newer JSON Schema version, no? |
|
(@ockham sorry I'm "hijacking" this PR to share my ongoing research — happy to post elsewhere). Investigated this a bit more by comparing JSON Schema (the version in use in REST API and the latest), the current status of the Field API & Block Attributes, and the HTML standard. The Field API needs to declare both data and UI. It sits in between a standard for data (JSON Schema) and a standard for UI (HTML). This explains why JSON Schema doesn't address rendering issues (format for date, locale for number, etc.), that the Field API and HTML need to handle.
|
|
@oandregal There's a lot of JSON Schema rendered tools which are the exact same level of abstraction as our Field API / DataViews, it might be good to check these for reference. |
|
Comparison of JSON Schema properties and Field API ones. I'll look at validation separately.
|
|
Here's validation:
|
|
This is what I've taken a look at and reported here:
After this research, here's what I think:
The only advantage of migration is that things look the same. But
There are other aspects to look at: block bindings / block types cc @ockham, the media roles experiment by @ntsekouras, etc. so I'll let other folks share their thoughts. |
|
That's very helpful research @oandregal! 💯 After reading all these, I'm not sure we can end up eventually using format like core does in Maybe the Regarding my media roles experiment, it seems it's also a different thing from JSON format and types. In general it tries to add semantic info in some attributes, which I cannot see how they fit in these |
JSON Schema is versioned by design. You do it by providing: "$schema": "http://json-schema.org/draft-04/schema#",That's the version that WordPress supports on the server at the moment. Using a different, more up-to-date version can be (and probably should be) supported. The detection mechanism is based on |
83305e3 to
1c596f3
Compare
| } | ||
| ] | ||
| }, | ||
| "format": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the path we went with in dataviews @oandregal
It seems like this PR might be diverging, and I'd prefer if we converge as discussed before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I didn't see these comments, so I posted my comment outside of this thread.)
The Field API provides more types for
type
Yes, this is a crucial difference to block attributes, where type is currently limited:
gutenberg/schemas/json/block.json
Lines 118 to 127 in ec6ec9e
| "enum": [ | |
| "null", | |
| "boolean", | |
| "object", | |
| "array", | |
| "string", | |
| "rich-text", | |
| "integer", | |
| "number" | |
| ] |
@youknowriad When you're saying you'd like things to converge, would you prefer if we extend the list of allowed block attribute types (to match the Fields API) and change existing block type definitions accordingly? E.g. "type":"uri" for url attributes in Button/Image/Navigation Link?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you prefer if we extend the list of allowed block attribute types (to match the Fields API) and change existing block type definitions accordingly?
That would be my preference yes. That said, this also means making the "Field API" stable which is not the case yet. So this is a decision that need to be intentional.
Basically, when we do this, breaking changes in terms of types in the dataviews package won't be possible anymore. cc @oandregal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the constraint would be freezing the types (e.g., date, datetime, integer, etc.), but we'd still be able to iterate on the rest of the Field API.
By looking at the type comparison across the standards (JSON Schema, Field API, HTML), I'm confident most of them will stay with us. For example, date, datetime, etc. exists across all systems — I don't expect those to go away. Then, there are some others (e.g., media, password, or color) that would benefit from more experimentation.
What if we started small, with a subset of them (e.g., date & datetime) and grow as required? What would be the types you'd anticipate needing for block bindings @ockham
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oandregal Gotcha, thank you for elaborating!
I’ve thought about this some more. To continue the train of thought about display formats (“YY-MM-DD” etc), I’m more and more convinced that for block attributes, display formats are very unlikely to ever be part of the type. Instead, they’re most likely to be kept separately. E.g. the Date block already has a format attribute that’s exactly for that. I think that this separation could be typical for how display formats would be handled by blocks: The actual data is stored in one attribute (datetime) that is display format agnostic; the display format can be set individually for each block instance via a UI control, and the setting is stored in a separate display format attribute:
<!-- wp:post-date {"datetime":"2025-12-03T17:48:48.089Z","format":"M j, Y"} /-->renders
Dec 3, 2025
In terms of convergence, the best we can do is probably align the non-display-format parts of type/format definitions for the Fields API and block attributes.
My concern with extending type to allow more granular types such as datetime or url (as demonstrated in #73624) is that it might have some side effects, especially when changing existing block attribute types. (There’s already some unit test errors as we seem to check against an allowlist of types for some block definitions related REST API endpoint.) I can continue that experiment to see if there are any showstoppers in Core. I can’t currently say if there might be 3rd-party code that relies, say, on an Image block’s href attribute to be a string rather than a url. What I don’t like about this strategy is that I don’t see a fallback plan if this turns out to be a no-go. Would we then simply never be able to add more granular type information to block attributes? (This is more a question for @youknowriad.) OTOH, using format in block attribute definitions for those formats/granular types would allow us to eschew those backwards-compatibility problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to dig deeper into this idea. Have you seen the experiment to use DataForm in content-only blocks? There's a lot of stuff there that's related to displaying the data and the UI for editing it. Would that not be relevant for block bindings?
Block Bindings are less about manual editing in the editor (block inspector) and more about connecting a given block attribute to a data source, e.g. connecting a Date block to a post’s publish date, or an Image block's url to a given post meta field. The UI currently looks like this:
By its nature, it's a bit more abstract than content-only editing (as editing those values -- which are provided by the aforementioned data sources -- isn't the main concern here).
Data items provided by data sources are typed, however -- filtering them by matching type for a given block attribute is the use case this PR is about. (For example, in the above screenshot, we'd probably only want the url_custom_field to show up for the url attribute.) Maybe DataViews could at some point come in handy for previewing data source items in this menu; currently, we're simply showing strings (e.g. Value of the custom field or #url-custom-field in the above screenshot). (Would it be possible use DataViews to show better previews?)
(Much like with block attributes, I think that the type/format of a data source item won't involve a display format.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual data is stored in one attribute (datetime) that is display format agnostic; the display format can be set individually for each block instance via a UI control, and the setting is stored in a separate display format attribute:
This is a good point but for me it doesn't neglect the need for blocks to say, I have a block that has an attribute that is for type "datetime" and I want to render an inspector control automatically in the UI (so DataForm) for that attribute with a given format.
Now, what you say is also a valid use case for both "blocks" and regular forms. Maybe you have a form where you want to first pick a "format" and then pick the "date" or something.
So basically we need to evolve the Fields API to support it but it doesn't neflect that we need to merge block attributes with Field API.
@oandregal I'm curious how you think we can evolve the fields API to support this. For me it does mean that "date" and "datetime" are probably the same field type. I'm curious if there's any reason that's forcing us to consider these two things as separate field types. If there is, then the solution is for blocks that need to switch between the two is to have two separate attributes and the one used is defined by a third "format" attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an update on the experiment with extending block attribute types: #73624 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious how you think we can evolve the fields API to support this. For me it does mean that "date" and "datetime" are probably the same field type. I'm curious if there's any reason that's forcing us to consider these two things as separate field types. If there is, then the solution is for blocks that need to switch between the two is to have two separate attributes and the one used is defined by a third "format" attribute.
The fact that you can use the display format to convert a datetime to a date is not what defines a type. Fields define a variety of functions (Edit, sort, etc.) and there's enough variety in these two types to support both. All standards that I've checked (JSON Schema + HTML) define them date and datetime as separate types (each with its own mechanism).
|
I’ve rebased this PR and would like to request reviews. Note that in the meantime, {
id: 'publishDate',
type: 'date',
label: 'Publish Date',
format: {
date: 'F j, Y',
weekStartsOn: 'monday',
},
}This is a different problem from what this PR is concerned with, where “format” is perhaps best described as a more granular data type. For example, the |
ddc3d37 to
e600b8e
Compare
What?
Introduce a new
formatfield for use in attribute definitions (e.g. inblock.json), and use it to filter block bindings source items based onformatcompatibility with bound attribute.Fixes #72446. Reverts #72712.
Why?
To remove incompatible source items from the block bindings UI for a given block bindings source.
For example, when we show the available items from the
core/post-datalist for the Date block'sdatetimeattribute, we only want to show thedateandmodifiedsource items, but notlink(which is anurland thus incompatible with adatetimefield).How?
By introducing a
formatfield in attribute definitions, and adding block bindings code to peruse it.Testing Instructions
datetimeattribute to expand the dropdown menu. It should contain the "Post Data" source.Screenshots or screencast
TODO
packages/editor/src/bindings/test/post-data.js(?)).