Skip to content

Commit bd46a75

Browse files
authored
feat: fallback to title when identifier_field is missing (#5775)
Co-authored-by: doompadee <[email protected]>
1 parent 0258a5c commit bd46a75

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/netlify-cms-core/src/reducers/__tests__/collections.spec.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ describe('collections', () => {
383383
});
384384

385385
describe('selectEntryCollectionTitle', () => {
386-
const entry = fromJS({ data: { title: 'entry title', otherField: 'other field' } });
386+
const entry = fromJS({
387+
data: { title: 'entry title', otherField: 'other field', emptyLinkTitle: '' },
388+
});
387389

388390
it('should return the entry title if set', () => {
389391
const collection = fromJS({
@@ -413,6 +415,24 @@ describe('collections', () => {
413415
expect(selectEntryCollectionTitle(collection, entry)).toEqual('other field');
414416
});
415417

418+
it('should return the entry title if identifier_field content is not defined in collection', () => {
419+
const collection = fromJS({
420+
identifier_field: 'missingLinkTitle',
421+
fields: [{ name: 'title' }, { name: 'otherField' }],
422+
});
423+
424+
expect(selectEntryCollectionTitle(collection, entry)).toEqual('entry title');
425+
});
426+
427+
it('should return the entry title if identifier_field content is empty', () => {
428+
const collection = fromJS({
429+
identifier_field: 'emptyLinkTitle',
430+
fields: [{ name: 'title' }, { name: 'otherField' }, { name: 'emptyLinkTitle' }],
431+
});
432+
433+
expect(selectEntryCollectionTitle(collection, entry)).toEqual('entry title');
434+
});
435+
416436
it('should return the entry label of a file collection', () => {
417437
const labelEntry = fromJS({
418438
slug: 'entry-name',

packages/netlify-cms-core/src/reducers/collections.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,14 @@ export function selectEntryCollectionTitle(collection: Collection, entry: EntryM
378378
// try to infer a title field from the entry data
379379
const entryData = entry.get('data');
380380
const titleField = selectInferedField(collection, 'title');
381-
return titleField && entryData.getIn(keyToPathArray(titleField));
381+
const result = titleField && entryData.getIn(keyToPathArray(titleField));
382+
383+
// if the custom field does not yield a result, fallback to 'title'
384+
if (!result && titleField !== 'title') {
385+
return entryData.getIn(keyToPathArray('title'));
386+
}
387+
388+
return result;
382389
}
383390

384391
export function selectDefaultSortableFields(

0 commit comments

Comments
 (0)