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: Move the logic for Template Part label to the block #28828

Merged
merged 1 commit into from
Feb 8, 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
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 @@ -20,6 +22,23 @@ export { metadata, name };
export const settings = {
title: _x( 'Template Part', 'block title' ),
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