Skip to content

Commit

Permalink
Blocks: Ensure that metadata registered on the server for core block …
Browse files Browse the repository at this point in the history
…is preserved on the client (#29213)

* export metadata, temporary placeholder to not override server values

* Add e2e test that ensure that filtered metadata is propagated to the client

* Fix issues raised by PHP linter

Co-authored-by: Kerry Liu <[email protected]>
  • Loading branch information
2 people authored and noisysocks committed Mar 1, 2021
1 parent 66efb75 commit ff0d663
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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 ] ) {
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();
} );
} );

0 comments on commit ff0d663

Please sign in to comment.