-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add __default binding for pattern overrides
#60694
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
Changes from 1 commit
9d45054
a099f03
bbca6e9
e89587a
8acdc99
f897e0f
77d7b20
afee53f
c6b1c4f
e5b834f
9b2dce8
bb2280e
50822b7
29a65a4
869d53d
b07b346
1cd20dd
5638bcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,13 +78,48 @@ function render_block_core_block( $attributes ) { | |
| * filter so that it is available when a pattern's inner blocks are | ||
| * rendering via do_blocks given it only receives the inner content. | ||
| */ | ||
| $has_pattern_overrides = isset( $attributes['content'] ); | ||
| $has_pattern_overrides = isset( $attributes['content'] ) && null !== get_block_bindings_source( 'core/pattern-overrides' ); | ||
| if ( $has_pattern_overrides ) { | ||
| $filter_block_context = static function ( $context ) use ( $attributes ) { | ||
| $context['pattern/overrides'] = $attributes['content']; | ||
| return $context; | ||
| }; | ||
| add_filter( 'render_block_context', $filter_block_context, 1 ); | ||
|
|
||
| // Add bindings for the default pattern overrides source. | ||
| $apply_default_pattern_overrides_bindings = static function ( $parsed_block ) { | ||
| $supported_block_attrs = array( | ||
| 'core/paragraph' => array( 'content' ), | ||
| 'core/heading' => array( 'content' ), | ||
| 'core/image' => array( 'id', 'url', 'title', 'alt' ), | ||
| 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), | ||
| ); | ||
|
||
|
|
||
| if ( | ||
| // Return early if the block isn't one of the supported block types, | ||
| ! isset( $supported_block_attrs[ $parsed_block['blockName'] ] ) || | ||
| // or doesn't have a name, | ||
| ! isset( $parsed_block['attrs']['metadata']['name'] ) || | ||
| // or doesn't have the default binding. | ||
| empty( $parsed_block['attrs']['metadata']['bindings']['__default'] ) || | ||
| // or the default binding isn't the pattern overrides source. | ||
| 'core/pattern-overrides' !== $parsed_block['attrs']['metadata']['bindings']['__default']['source'] | ||
| ) { | ||
| return $parsed_block; | ||
| } | ||
|
|
||
| $bindings = array(); | ||
| foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) { | ||
| $bindings[ $attribute_name ] = array( 'source' => 'core/pattern-overrides' ); | ||
| } | ||
| $parsed_block['attrs']['metadata']['bindings'] = array_merge( | ||
| $bindings, | ||
| $parsed_block['attrs']['metadata']['bindings'] | ||
| ); | ||
kevin940726 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return $parsed_block; | ||
| }; | ||
| add_filter( 'render_block_data', $apply_default_pattern_overrides_bindings, 10, 1 ); | ||
|
||
| } | ||
|
|
||
| $content = do_blocks( $content ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new filter should be removed after rendering blocks, similar to how it happens to remove_filter( 'render_block_data', $apply_default_pattern_overrides_bindings );
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, I've pushed a commit that does that 👍
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though I've now removed this code entirely as well. 😄 |
||
|
|
||
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.
As far as I understand, the #60694 (comment) from @SantosGuillamot is about the changes applied in the general processing of block bindings in the block editor. At the moment, the implementation proposed makes
__defaulta keyword that would work with every possible type of block binding source on the client, but there is no matching logic on the server that would make it universally applicable. What alternatives would be possible to limit it to Pattern Overrides only?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.
With the
getValueimplementation, I don't think there's any way to move this logic into pattern overrides source itself. At the moment this file checks each attribute individually so it expects themetadata.bindingsattribute to use the same attribute name as the block attribute.If it were
getValuesinstead, I think it might be possible, as perhaps the code would encounter__defaultin themetadata.bindings, know that there's an active binding of some kind, and defer completely to thegetValuesimplementation to return the matching values for each attribute. WithingetValuesthe code could expand the__defaultbinding to the complete supported attribute listBut probably the easiest implementation is to add some hard-coding in
useBindingAttributesso that__defaultonly works when it's set tocore/pattern-overrides.I realise that there's also ongoing efforts to refactor this into the store right now so the implementation might change.
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.
Yes, that works for now 👍🏻
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've made those changes. Also tried to make the changes to
useBindingAttributesa lot more minimal so its easier to port to a different implementation, now the__defaultbinding is handled by one function, and the rest of the code is the same.