Skip to content
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

[Pattern overrides] Allow multiple attributes overrides #57573

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading