Skip to content

Commit

Permalink
Allow multiple attributes overrides for patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Jan 5, 2024
1 parent 7946a0d commit 92d4492
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
8 changes: 6 additions & 2 deletions lib/experimental/connection-sources/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
},
);
16 changes: 11 additions & 5 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -111,7 +114,7 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {
}

function getOverridesFromBlocks( blocks, defaultValues ) {
/** @type {Record<string, unknown>} */
/** @type {Record<string, Record<string, unknown>>} */
const overrides = {};
for ( const block of blocks ) {
Object.assign(
Expand All @@ -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 ];
}
}
}
Expand Down

0 comments on commit 92d4492

Please sign in to comment.