Skip to content

Commit

Permalink
Blocks: Move the logic for Template Part label to the block (#28828)
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo authored Feb 8, 2021
1 parent 64bd23a commit eeea281
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
21 changes: 20 additions & 1 deletion packages/block-library/src/template-part/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { startCase } from 'lodash';
/**
* WordPress dependencies
*/
import { store as coreDataStore } from '@wordpress/core-data';
import { select } from '@wordpress/data';
import { __, _x } from '@wordpress/i18n';

/**
Expand All @@ -23,6 +25,23 @@ export const settings = {
'Edit the different global regions of your site, like the header, footer, sidebar, or create your own.'
),
keywords: [ __( 'template part' ) ],
__experimentalLabel: ( { slug } ) => startCase( slug ),
__experimentalLabel: ( { slug, theme } ) => {
// Attempt to find entity title if block is a template part.
// Require slug to request, otherwise entity is uncreated and will throw 404.
if ( ! slug ) {
return;
}

const entity = select( coreDataStore ).getEntityRecord(
'postType',
'wp_template_part',
theme + '//' + slug
);
if ( ! entity ) {
return;
}

return startCase( entity.title?.rendered || entity.slug );
},
edit,
};
16 changes: 1 addition & 15 deletions packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { every, has, isFunction, isString, startCase, reduce } from 'lodash';
import { every, has, isFunction, isString, reduce } from 'lodash';
import { default as tinycolor, mostReadable } from 'tinycolor2';

/**
Expand All @@ -10,7 +10,6 @@ import { default as tinycolor, mostReadable } from 'tinycolor2';
import { Component, isValidElement } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { select } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -141,19 +140,6 @@ export function normalizeBlockType( blockTypeOrName ) {
* @return {string} The block label.
*/
export function getBlockLabel( blockType, attributes, context = 'visual' ) {
// Attempt to find entity title if block is a template part.
// Require slug to request, otherwise entity is uncreated and will throw 404.
if ( 'core/template-part' === blockType.name && attributes.slug ) {
const entity = select( 'core' ).getEntityRecord(
'postType',
'wp_template_part',
attributes.theme + '//' + attributes.slug
);
if ( entity ) {
return startCase( entity.title?.rendered || entity.slug );
}
}

const { __experimentalLabel: getLabel, title } = blockType;

const label = getLabel && getLabel( attributes, { context } );
Expand Down

0 comments on commit eeea281

Please sign in to comment.