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

Check possible identifier values for a valid one to use for {{slug}} #1757

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ npm-debug.log
.tern-project
yarn-error.log
.vscode/
.idea/
manifest.yml
.imdone/
website/data/contributors.json
Expand Down
8 changes: 6 additions & 2 deletions packages/netlify-cms-core/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
selectAllowNewEntries,
selectAllowDeletion,
selectFolderEntryExtension,
selectIdentifier,
selectIdentifierCandidates,
selectInferedField,
} from 'Reducers/collections';
import { createEntry } from 'ValueObjects/Entry';
Expand Down Expand Up @@ -40,7 +40,11 @@ const slugFormatter = (collection, entryData, slugConfig) => {
const template = collection.get('slug') || '{{slug}}';
const date = new Date();

const identifier = entryData.get(selectIdentifier(collection));
const identifierFieldCandidates = selectIdentifierCandidates(collection);
const identifier = identifierFieldCandidates
.map(fieldName => entryData.get(fieldName))
.find(value => value);

if (!identifier) {
throw new Error('Collection must have a field name that is a valid entry identifier');
}
Expand Down
5 changes: 3 additions & 2 deletions packages/netlify-cms-core/src/reducers/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ export const selectAllowDeletion = collection =>
selectors[collection.get('type')].allowDeletion(collection);
export const selectTemplateName = (collection, slug) =>
selectors[collection.get('type')].templateName(collection, slug);
export const selectIdentifier = collection => {
export const selectIdentifierCandidates = collection => {
const fieldNames = collection.get('fields').map(field => field.get('name'));
return IDENTIFIER_FIELDS.find(id => fieldNames.find(name => name.toLowerCase().trim() === id));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change needed here is doing a filter instead of a find, and returning the array of field names that occurs in both. We could greatly simplify this by using lodash.intersection. This allows the order of IDENTIFIER_FIELDS to dictate which field is used, otherwise this would be a breaking change.

const identifierFieldsSet = new Set(IDENTIFIER_FIELDS);
return fieldNames.filter(name => identifierFieldsSet.has(name.toLowerCase().trim()));
// There must be a field whose `name` matches one of the IDENTIFIER_FIELDS.
};
export const selectInferedField = (collection, fieldName) => {
Expand Down