Skip to content

Commit

Permalink
Template Part - fix labels shown - use title instead of slug (#28330)
Browse files Browse the repository at this point in the history
* use title instead of slug

* add special template-part check for

* fix bad request made while creating new template part

* add comment
  • Loading branch information
Addison-Stavlo authored Jan 20, 2021
1 parent a5a35f7 commit 3bfea59
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
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
16 changes: 15 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,19 @@ 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
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

0 comments on commit 3bfea59

Please sign in to comment.