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

Template Part - fix labels shown - use title instead of slug #28330

Merged
merged 4 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function TemplatePartItem( {
onClose,
composite,
} ) {
const { slug, theme, title } = templatePart;
const {
slug,
theme,
title: { rendered: title },
} = templatePart;
// The 'raw' property is not defined for a brief period in the save cycle.
// The fallback prevents an error in the parse function while saving.
const content = templatePart.content.raw || '';
Expand All @@ -49,7 +53,7 @@ function TemplatePartItem( {
sprintf(
/* translators: %s: template part title. */
__( 'Template Part "%s" inserted.' ),
title.rendered
title || slug
),
{
type: 'snackbar',
Expand All @@ -70,12 +74,12 @@ function TemplatePartItem( {
}
} }
tabIndex={ 0 }
aria-label={ templatePart.slug }
aria-label={ title || slug }
{ ...composite }
>
<BlockPreview blocks={ blocks } />
<div className="wp-block-template-part__selection-preview-item-title">
{ templatePart.slug }
{ title || slug }
</div>
</CompositeItem>
);
Expand Down
14 changes: 13 additions & 1 deletion 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 } from 'lodash';
import { every, has, isFunction, isString, startCase } from 'lodash';
import { default as tinycolor, mostReadable } from 'tinycolor2';

/**
Expand All @@ -10,6 +10,7 @@ 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 @@ -140,6 +141,17 @@ export function normalizeBlockType( blockTypeOrName ) {
* @return {string} The block label.
*/
export function getBlockLabel( blockType, attributes, context = 'visual' ) {
if ( 'core/template-part' === blockType.name ) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export default function TemplateNavigationItem( { item } ) {
( select ) =>
'wp_template' === item.type
? select( 'core/editor' ).__experimentalGetTemplateInfo( item )
: { title: item?.slug, description: '' },
: {
title: item?.title?.rendered || item?.slug,
description: '',
},
[]
);
const { setTemplate, setTemplatePart } = useDispatch( 'core/edit-site' );
Expand Down