-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block API: Add Block Context support #21467
Conversation
Size Change: +566 B (0%) Total Size: 842 kB
ℹ️ View Unchanged
|
Nice work! This looks really solid and the API is applicable for things outside of just the Post and Post Content blocks, too. For example, in #21075, we need the Navigation Link block to be able to access styling attributes that are defined by the parent Navigation block. I've only just caught up on the discussion in #19685, but let me try anyway to review this and answer your questions. (And apologies if I'm slow!)
I like the balance between simplicity and "correctness" here.
I'm hesitant about adding a new class WP_Block implements ArrayAccess {
public $attributes;
public $content;
public $context;
public function offsetGet( $key ) {
return $attributes[ $key ];
}
}
...
render_callback( $block, $block->content ); Agree with you that this shouldn't block this PR, though. We have until WP 5.5 to make adjustments to the API.
My only worry here is that block developers may come to assume that
I think best to leave this out until Gutenberg has a practical use-case for it.
Whatever small risk of conflict there is is mitigated by the explicitness of having
Yes I think so. It's nice for block authors to just be able to write |
docs/designers-developers/developers/block-api/block-context.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/block-api/block-context.md
Outdated
Show resolved
Hide resolved
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 really like the API and this will help massively for some work I'm undertaking to tidy up the Navigation block (#21075).
I feel like the API works well for blocks that have a very well defined relationship, like Navigation and Navigation Link, the use case here is that colors (particularly background color) can be defined on the parent and propagate to the child.
I've wondered about a future where we wanted to bring something like the Search block into the allowed blocks for Navigation and how something like that might work. Potentially it might have to be a separate block. Variations is also something I considered, whether a variation could use context where the original block might not. Not something to worry about right now, but just some thoughts.
docs/designers-developers/developers/block-api/block-context.md
Outdated
Show resolved
Hide resolved
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.
Excellent work. I like the direction, and I agree that we can start with simplified approach that assumes that exposed data with context
by default gets overridden when the same field gets exposed deeper in the tree. The fact that we would use the pair context
and providesContext
leaves some room for further refinements. Let's see if this ever becomes an issue.
docs/designers-developers/developers/block-api/block-context.md
Outdated
Show resolved
Hide resolved
Thanks everyone for the feedback here! I'll work to address each item individually. |
@noisysocks I might not be understanding correctly, but is there something about I would be happy if we can find a way to integrate this into the function signature of I've personally felt this is partly what's blocked an implementation thus far. The choice to use a global is in many ways intended to be consistent with the existence of a |
It's a fair worry. It ties in to a historical desire to try to limit assumptions from blocks about it being within the context of some post, at least where appropriate (e.g. a paragraph is static, self-contained, and needn't worry about the existence of a post). Conversely, some blocks we've made the conscious decision that this contextual awareness is appropriate, like in Navigation block, or new full-site editing blocks like "Post Title". These will need to be aware of some external factors like the current post, or site settings. To your overall point, I think we should generally want to continue to push developers to consider absence of context as a default position, and make a conscious choice to become "aware" only as appropriate. Whether we've put sufficient safeguards / guidelines in place to ensure this happens is yet to be seen, and could be further improved (documentation, technical restrictions, etc). |
One other thing I failed to mention: This requires that there be upstream revisions to the following function: Since we are now adding new settings properties It works okay in this pull request, largely because we're importing These keys aren't filterable, so there's no way to extend the set from Gutenberg. An upstream ticket should propose to add new properties (once this proposal is settled), and optionally make them filterable. |
Status update:
|
While it was the intention to retain full backward-compatibility, there have been observed some incompatibilities resulting from the changes here to I've summarized the problem and potential solutions at #21797. |
Noting that I added WordPress Core Trac ticket to the description: https://core.trac.wordpress.org/ticket/49927. |
Backports functionality added in Gutenberg in the following PRs: - WordPress/gutenberg#21467 - WordPress/gutenberg#21925 It's a few ideas related to block rendering and the provided block value, which is particularly impactful for work around block context. Props aduth, TimothyBJacobs, noisysocks, epiqueras, youknowriad, talldanwp, zebulan. Fixes #49926. git-svn-id: https://develop.svn.wordpress.org/trunk@48159 602fd350-edb4-49c9-b593-d223f7449a82
Backports functionality added in Gutenberg in the following PRs: - WordPress/gutenberg#21467 - WordPress/gutenberg#21925 It's a few ideas related to block rendering and the provided block value, which is particularly impactful for work around block context. Props aduth, TimothyBJacobs, noisysocks, epiqueras, youknowriad, talldanwp, zebulan. Fixes #49926. git-svn-id: https://develop.svn.wordpress.org/trunk@48159 602fd350-edb4-49c9-b593-d223f7449a82
Backports functionality added in Gutenberg in the following PRs: - WordPress/gutenberg#21467 - WordPress/gutenberg#21925 It's a few ideas related to block rendering and the provided block value, which is particularly impactful for work around block context. Props aduth, TimothyBJacobs, noisysocks, epiqueras, youknowriad, talldanwp, zebulan. Fixes #49926. Built from https://develop.svn.wordpress.org/trunk@48159 git-svn-id: http://core.svn.wordpress.org/trunk@47928 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Backports functionality added in Gutenberg in the following PRs: - WordPress/gutenberg#21467 - WordPress/gutenberg#21925 It's a few ideas related to block rendering and the provided block value, which is particularly impactful for work around block context. Props aduth, TimothyBJacobs, noisysocks, epiqueras, youknowriad, talldanwp, zebulan. Fixes #49926. Built from https://develop.svn.wordpress.org/trunk@48159 git-svn-id: https://core.svn.wordpress.org/trunk@47928 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Backports functionality added in Gutenberg in the following PRs: - WordPress/gutenberg#21467 - WordPress/gutenberg#21925 It's a few ideas related to block rendering and the provided block value, which is particularly impactful for work around block context. Props aduth, TimothyBJacobs, noisysocks, epiqueras, youknowriad, talldanwp, zebulan. Fixes #49926. git-svn-id: https://develop.svn.wordpress.org/trunk@48159 602fd350-edb4-49c9-b593-d223f7449a82
Closes #19685
Closes #4671
Previously: #19572
WordPress Trac ticket: https://core.trac.wordpress.org/ticket/49927.
This pull request seeks to implement the means for a block to provide and consume contextual values. In many ways, it is based on the earlier work of #19572, but there are some key differences in the specifics of how context is defined. I've included a fair bit of documentation here as well, which was useful for me in validating ("rubber duck"-ing) the underlying implementation. This documentation should prove useful for understanding what it is, how it works, and why it's proposed to be implemented the way it is.
Differences from #19572:
attributes
schema, as its ownprovidesContext
property.Server-side, the implementation proposed here does not modify the function signatures of eitherIn later revisions of this pull request, an enhancement was achieved where the fullrender_block
or a block'srender_callback
. Instead, both the block context of the current hierarchy and the block itself are assigned as globals which can be referenced in therender_callback
.$block
instance is passed as an argument torender_callback
, retaining backward compatibility by implementingArrayAccess
where the instance's array members are short-hand to its own attributes. See Block API: Add Block Context support #21467 (comment) for more information.This is future-compatible with revisions to the function signatures, if there's a desire to go this route. I did not feel that this decision should block the implementation. I've remarked to the future possibility of revised function signatures in the above-referenced documentation.Later changes of the pull request implemented said future-compatible revisions.This also solves requirements where some developers have sought to access other properties ofLater changes of the pull request serve as a solution to this need.$block
while withinrender_callback
, avoiding needing to resort to workarounds usingrender_block
hook (e.g. Do advanced rendering by hooking into render_block for navigation block #19991).InnerBlocks
, as an optimization to avoid establishing a context unnecessarily if there are no children to consume from it.Open Questions:
render_block
already manipulates and assigns the$post
global. I'd worry if this might lead to a situation where there's not clear expectations around what should or shouldn't be available by default. "Post" is a particularly special case and may be fine to assume it would be unique in this regard.save
? I've assumed not, given that this may lead to unintended invalidations, and that at least for the current set of use-cases, context would be evaluated server-side in arender_callback
.context
object always be provided, even when the block does not define any context to consume? My decision to pass an empty object is largely for developer experience, anticipating potential errors if a developer were to assume that the object is present and attempt unguarded access to properties on that (possibly-undefined) value.In Progress:
I am observing PHP warning notices in the "Site Editor" page related to these changes, which I've yet to resolve. They appear to be related to the assignment of theFixed, see Block API: Add Block Context support #21467 (comment).$block
global in the overriddenrender_block
implementation.Testing Instructions:
Given that this pull request only ports the Post Title experimental full-site editing block, testing can be a bit cumbersome at the moment, especially if you've not yet defined templates for full-site editing mode.
My recommendation for testing is as follows: