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

Blocks: Ensure that metadata registered on the server for core block is preserved on the client #29213

Merged
merged 3 commits into from
Feb 23, 2021
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
5 changes: 5 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ export const serverSideBlockDefinitions = {};
// eslint-disable-next-line camelcase
export function unstable__bootstrapServerSideBlockDefinitions( definitions ) {
for ( const blockName of Object.keys( definitions ) ) {
// Don't overwrite if already set. It covers the case when metadata
// was initialized from the server.
if ( serverSideBlockDefinitions[ blockName ] ) {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
serverSideBlockDefinitions[ blockName ] = mapKeys(
pickBy( definitions[ blockName ], ( value ) => ! isNil( value ) ),
( value, key ) => camelCase( key )
Expand Down
28 changes: 28 additions & 0 deletions packages/e2e-tests/plugins/register-block-type-hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Plugin Name: Gutenberg Test Register Block Type Hooks
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-register-block-type-hooks
*/

/**
* Changes the category for the paragraph block.
*
* @param array $metadata Array of metadata for registering a block type.
*
* @return array Filtered metadata for registering a block type.
*/
function gutenberg_test_block_type_metadata( $metadata ) {
if ( 'core/paragraph' !== $metadata['name'] ) {
return $metadata;
}

return array_merge(
$metadata,
array( 'category' => 'widgets' )
);
}

add_filter( 'block_type_metadata', 'gutenberg_test_block_type_metadata' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import {
activatePlugin,
createNewPost,
deactivatePlugin,
openGlobalBlockInserter,
} from '@wordpress/e2e-test-utils';

describe( 'Register block type hooks', () => {
beforeEach( async () => {
await activatePlugin( 'gutenberg-test-register-block-type-hooks' );
await createNewPost();
} );

afterEach( async () => {
await deactivatePlugin( 'gutenberg-test-register-block-type-hooks' );
} );

it( 'has a custom category for Paragraph block', async () => {
await openGlobalBlockInserter();

const widgetsCategory = await page.$(
'.block-editor-block-types-list[aria-label="Widgets"]'
);

expect(
await widgetsCategory.$( '.editor-block-list-item-paragraph' )
).toBeDefined();
} );
} );