Skip to content

Commit

Permalink
Handle undefined attributes as null in block bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Jan 18, 2024
1 parent d159752 commit c34e1ad
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/block-supports/pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function gutenberg_register_pattern_support( $block_type ) {
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/image' => array( 'url', 'title', 'alt' ),
'core/button' => array( 'url', 'text' ),
'core/button' => array( 'url', 'text', 'linkTarget' ),
);
$pattern_support = array_key_exists( $block_type->name, $allowed_blocks );

Expand Down
4 changes: 4 additions & 0 deletions lib/experimental/block-bindings/class-wp-block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* Core class used to define supported blocks, register sources, and populate HTML with content from those sources.
*/
class WP_Block_Bindings {
/**
* A singleton flag to represent skipping processing the attribute.
*/
const SKIP = array();

/**
* Holds the registered block bindings sources, keyed by source identifier.
Expand Down
4 changes: 2 additions & 2 deletions lib/experimental/block-bindings/sources/pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
if ( function_exists( 'wp_block_bindings_register_source' ) ) {
$pattern_source_callback = function ( $source_attrs, $block_instance, $attribute_name ) {
if ( ! _wp_array_get( $block_instance->attributes, array( 'metadata', 'id' ), false ) ) {
return null;
return WP_Block_Bindings::SKIP;
}
$block_id = $block_instance->attributes['metadata']['id'];
return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null );
return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), WP_Block_Bindings::SKIP );
};
wp_block_bindings_register_source(
'pattern_attributes',
Expand Down
4 changes: 2 additions & 2 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function gutenberg_process_block_bindings( $block_content, $block, $block_instan
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/image' => array( 'url', 'title', 'alt' ),
'core/button' => array( 'url', 'text' ),
'core/button' => array( 'url', 'text', 'linkTarget' ),
);

// If the block doesn't have the bindings property or isn't one of the allowed block types, return.
Expand Down Expand Up @@ -153,7 +153,7 @@ function gutenberg_process_block_bindings( $block_content, $block, $block_instan
}
$source_value = $source_callback( $source_args, $block_instance, $binding_attribute );
// If the value is null, process next attribute.
if ( is_null( $source_value ) ) {
if ( WP_Block_Bindings::SKIP === $source_value ) {
continue;
}

Expand Down
15 changes: 10 additions & 5 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,14 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {
defaultValues[ blockId ] ??= {};
defaultValues[ blockId ][ attributeKey ] =
block.attributes[ attributeKey ];
if ( overrides[ blockId ]?.[ attributeKey ] !== undefined ) {
if (
overrides[ blockId ] &&
Object.hasOwn( overrides[ blockId ], attributeKey )
) {
newAttributes[ attributeKey ] =
overrides[ blockId ][ attributeKey ];
overrides[ blockId ][ attributeKey ] === undefined
? null
: overrides[ blockId ][ attributeKey ];
}
}
return {
Expand Down Expand Up @@ -134,10 +139,10 @@ function getOverridesFromBlocks( blocks, defaultValues ) {
defaultValues[ blockId ][ attributeKey ]
) {
overrides[ blockId ] ??= {};
// TODO: We need a way to represent `undefined` in the serialized overrides.
// Also see: https://github.com/WordPress/gutenberg/pull/57249#discussion_r1452987871
overrides[ blockId ][ attributeKey ] =
block.attributes[ attributeKey ];
block.attributes[ attributeKey ] === undefined
? null
: block.attributes[ attributeKey ];
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/patterns/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const PARTIAL_SYNCING_SUPPORTED_BLOCKS = {
'core/button': {
text: __( 'Text' ),
url: __( 'URL' ),
linkTarget: __( 'Link Target' ),
},
'core/image': {
url: __( 'URL' ),
Expand Down

0 comments on commit c34e1ad

Please sign in to comment.