Skip to content

Commit 1752043

Browse files
authored
fix: sanitize collection labels to inherit defaults when only a partial config is provided (#13944)
When only a partial `labels` config is defined on a collection, the collection defaults do not apply as expected. This leads to undefined `singular` or `plural` properties that render as either empty or untranslated strings on the front-end. For example: ```ts import type { CollectionConfig } from 'payload' export MyCollection: CollectionConfig = { // ... labels: { plural: 'Pages', // Notice that `singular` is excluded here }, } ``` This renders empty or untranslated strings throughout the admin panel, here are a couple examples: <img width="326" height="211" alt="Screenshot 2025-09-26 at 10 27 40 AM" src="https://github.com/user-attachments/assets/3872c4dd-0dac-4c1c-b417-61ddd042bbb8" /> <img width="330" height="267" alt="Screenshot 2025-09-26 at 10 27 51 AM" src="https://github.com/user-attachments/assets/78772405-b5f3-45fa-9bf0-bc078f1ba976" /> --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1211478736160147
1 parent 2cc34d1 commit 1752043

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

packages/payload/src/collections/config/sanitize.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ export const sanitizeCollection = async (
158158
}
159159
}
160160

161-
sanitized.labels = sanitized.labels || formatLabels(sanitized.slug)
161+
const defaultLabels = formatLabels(sanitized.slug)
162+
163+
sanitized.labels = {
164+
plural: sanitized.labels?.plural || defaultLabels.plural,
165+
singular: sanitized.labels?.singular || defaultLabels.singular,
166+
}
162167

163168
if (sanitized.versions) {
164169
if (sanitized.versions === true) {

packages/payload/src/utilities/formatLabels.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const toWords = (inputString: string, joinWords = false): string => {
2222

2323
const formatLabels = (slug: string): { plural: string; singular: string } => {
2424
const words = toWords(slug)
25+
2526
return isPlural(slug)
2627
? {
2728
plural: words,

test/config/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export default buildConfigWithDefaults({
1414
collections: [
1515
{
1616
slug: 'pages',
17+
labels: {
18+
// Purposefully exclude `singular` to test default inheritance
19+
plural: 'Pages',
20+
},
1721
access: {
1822
create: () => true,
1923
delete: () => true,

test/config/int.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ describe('Config', () => {
126126
description: 'The blockOne of this page',
127127
})
128128
})
129+
130+
it('properly merges collection.labels with defaults', () => {
131+
const [collection] = payload.config.collections
132+
expect(collection?.labels).toEqual({ plural: 'Pages', singular: 'Page' })
133+
})
129134
})
130135

131136
describe('global config', () => {

0 commit comments

Comments
 (0)