Skip to content

Commit

Permalink
Adds block supports for metadata (#43986)
Browse files Browse the repository at this point in the history
* Adds metadata supports

* Correct spelling

Co-authored-by: Ben Dwyer <ben@scruffian.com>

* Add explanatory comment

Co-authored-by: Ben Dwyer <ben@scruffian.com>
Co-authored-by: Jorge Costa <jorge.costa@developer.pt>
  • Loading branch information
3 people authored Sep 12, 2022
1 parent dff616f commit 9e08674
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/compat/wordpress-6.1/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,28 @@ function gutenberg_block_type_metadata_render_template( $settings, $metadata ) {
return $settings;
}
add_filter( 'block_type_metadata_settings', 'gutenberg_block_type_metadata_render_template', 10, 2 );

/**
* Registers the metadata block attribute for block types.
*
* Once 6.1 is the minimum supported WordPress version for the Gutenberg
* plugin, this shim can be removed
*
* @param array $args Array of arguments for registering a block type.
* @return array $args
*/
function gutenberg_register_metadata_attribute( $args ) {
// Setup attributes if needed.
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
$args['attributes'] = array();
}

if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) {
$args['attributes']['metadata'] = array(
'type' => 'object',
);
}

return $args;
}
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' );
2 changes: 2 additions & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import './font-size';
import './border';
import './layout';
import './content-lock-ui';
import './metadata';
import './metadata-name';

export { useCustomSides } from './dimensions';
export { getBorderClassesAndStyles, useBorderProps } from './use-border-props';
Expand Down
48 changes: 48 additions & 0 deletions packages/block-editor/src/hooks/metadata-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { hasBlockMetadataSupport } from './metadata';

/**
* Filters registered block settings, adding an `__experimentalLabel` callback if one does not already exist.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
export function addLabelCallback( settings ) {
// If blocks provide their own label callback, do not override it.
if ( settings.__experimentalLabel ) {
return settings;
}

const supportsBlockNaming = hasBlockMetadataSupport(
settings,
'name',
false // default value
);

// Check whether block metadata is supported before using it.
if ( supportsBlockNaming ) {
settings.__experimentalLabel = ( attributes, { context } ) => {
const { metadata } = attributes;

// In the list view, use the block's name attribute as the label.
if ( context === 'list-view' && metadata?.name ) {
return metadata.name;
}
};
}

return settings;
}

addFilter(
'blocks.registerBlockType',
'core/metadata/addLabelCallback',
addLabelCallback
);
64 changes: 64 additions & 0 deletions packages/block-editor/src/hooks/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { getBlockSupport } from '@wordpress/blocks';

const META_ATTRIBUTE_NAME = 'metadata';

export function hasBlockMetadataSupport( blockType, feature = '' ) {
const support = getBlockSupport( blockType, '__experimentalMetadata' );
return !! ( true === support || support?.[ feature ] );
}

/**
* Filters registered block settings, extending attributes to include `metadata`.
*
* see: https://github.com/WordPress/gutenberg/pull/40393/files#r864632012
*
* @param {Object} blockTypeSettings Original block settings.
* @return {Object} Filtered block settings.
*/
export function addMetaAttribute( blockTypeSettings ) {
// Allow blocks to specify their own attribute definition with default values if needed.
if ( blockTypeSettings?.attributes?.[ META_ATTRIBUTE_NAME ]?.type ) {
return blockTypeSettings;
}

const supportsBlockNaming = hasBlockMetadataSupport(
blockTypeSettings,
'name',
false
);

if ( supportsBlockNaming ) {
blockTypeSettings.attributes = {
...blockTypeSettings.attributes,
[ META_ATTRIBUTE_NAME ]: {
type: 'object',
},
};
}

return blockTypeSettings;
}

export function addSaveProps( extraProps, blockType, attributes ) {
if ( hasBlockMetadataSupport( blockType ) ) {
extraProps[ META_ATTRIBUTE_NAME ] = attributes[ META_ATTRIBUTE_NAME ];
}

return extraProps;
}

addFilter(
'blocks.registerBlockType',
'core/metadata/addMetaAttribute',
addMetaAttribute
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/metadata/save-props',
addSaveProps
);

0 comments on commit 9e08674

Please sign in to comment.