From 92d4492b2ad9ef3335c2483fc75c9ab744181b2f Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Fri, 5 Jan 2024 13:48:25 +0800 Subject: [PATCH] Allow multiple attributes overrides for patterns --- lib/experimental/blocks.php | 2 +- lib/experimental/connection-sources/index.php | 8 ++++++-- packages/block-library/src/block/edit.js | 16 +++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 88e46b478389d2..d4bb6c9b4586eb 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -150,7 +150,7 @@ function gutenberg_render_block_connections( $block_content, $block, $block_inst continue; } - $custom_value = $connection_sources[ $attribute_value['source'] ]( $block_instance ); + $custom_value = $connection_sources[ $attribute_value['source'] ]( $block_instance, $attribute_name ); } else { // If the attribute does not specify the name of the custom field, skip it. if ( ! isset( $attribute_value['value'] ) ) { diff --git a/lib/experimental/connection-sources/index.php b/lib/experimental/connection-sources/index.php index bf89ba177b6e94..4f9e06cb13b945 100644 --- a/lib/experimental/connection-sources/index.php +++ b/lib/experimental/connection-sources/index.php @@ -12,8 +12,12 @@ // if it doesn't, `get_post_meta()` will just return an empty string. return get_post_meta( $block_instance->context['postId'], $meta_field, true ); }, - 'pattern_attributes' => function ( $block_instance ) { + 'pattern_attributes' => function ( $block_instance, $attribute_name ) { $block_id = $block_instance->attributes['metadata']['id']; - return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id ), false ); + return _wp_array_get( + $block_instance->context, + array( 'pattern/overrides', $block_id, $attribute_name ), + false + ); }, ); diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index 607f073323d996..ff0127e203a46c 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -97,9 +97,12 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) { const attributes = getPartiallySyncedAttributes( block ); const newAttributes = { ...block.attributes }; for ( const attributeKey of attributes ) { - defaultValues[ blockId ] = block.attributes[ attributeKey ]; + defaultValues[ blockId ] ??= {}; + defaultValues[ blockId ][ attributeKey ] = + block.attributes[ attributeKey ]; if ( overrides[ blockId ] ) { - newAttributes[ attributeKey ] = overrides[ blockId ]; + newAttributes[ attributeKey ] = + overrides[ blockId ][ attributeKey ]; } } return { @@ -111,7 +114,7 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) { } function getOverridesFromBlocks( blocks, defaultValues ) { - /** @type {Record} */ + /** @type {Record>} */ const overrides = {}; for ( const block of blocks ) { Object.assign( @@ -123,9 +126,12 @@ function getOverridesFromBlocks( blocks, defaultValues ) { const attributes = getPartiallySyncedAttributes( block ); for ( const attributeKey of attributes ) { if ( - block.attributes[ attributeKey ] !== defaultValues[ blockId ] + block.attributes[ attributeKey ] !== + defaultValues[ blockId ][ attributeKey ] ) { - overrides[ blockId ] = block.attributes[ attributeKey ]; + overrides[ blockId ] ??= {}; + overrides[ blockId ][ attributeKey ] = + block.attributes[ attributeKey ]; } } }