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

Add support for setting custom identifier field #1543

Merged
merged 2 commits into from
Nov 1, 2018
Merged
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
4 changes: 3 additions & 1 deletion packages/netlify-cms-core/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const slugFormatter = (collection, entryData, slugConfig) => {

const identifier = entryData.get(selectIdentifier(collection));
if (!identifier) {
throw new Error('Collection must have a field name that is a valid entry identifier');
throw new Error(
'Collection must have a field name that is a valid entry identifier, or must have `identifier_field` set',
);
}

const slug = template
Expand Down
4 changes: 2 additions & 2 deletions packages/netlify-cms-core/src/constants/configSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const getConfigSchema = () => ({
fields: {
contains: {
properties: {
name: { enum: IDENTIFIER_FIELDS },
name: { enum: [{ $data: '3/identifier_field' }, ...IDENTIFIER_FIELDS] },
},
},
},
Expand Down Expand Up @@ -169,7 +169,7 @@ class ConfigError extends Error {
* the config that is passed in.
*/
export function validateConfig(config) {
const ajv = new AJV({ allErrors: true, jsonPointers: true });
const ajv = new AJV({ allErrors: true, jsonPointers: true, $data: true });
ajvErrors(ajv);

const valid = ajv.validate(getConfigSchema(), config);
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 @@ -113,9 +113,10 @@ export const selectAllowDeletion = collection =>
export const selectTemplateName = (collection, slug) =>
selectors[collection.get('type')].templateName(collection, slug);
export const selectIdentifier = collection => {
const identifier = collection.get('identifier_field');
const indentifierFields = identifier ? [identifier, ...IDENTIFIER_FIELDS] : IDENTIFIER_FIELDS;
const fieldNames = collection.get('fields').map(field => field.get('name'));
return IDENTIFIER_FIELDS.find(id => fieldNames.find(name => name.toLowerCase().trim() === id));
// There must be a field whose `name` matches one of the IDENTIFIER_FIELDS.
return indentifierFields.find(id => fieldNames.find(name => name.toLowerCase().trim() === id));
};
export const selectInferedField = (collection, fieldName) => {
const inferableField = INFERABLE_FIELDS[fieldName];
Expand Down
17 changes: 16 additions & 1 deletion website/content/docs/collection-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Folder collections represent one or more files with the same format, fields, and

Unlike file collections, folder collections have the option to allow editors to create new items in the collection. This is set by the boolean `create` field.

**Note:** Folder collections must have at least one field with the name `title` for creating new entry slugs. That field should use the default `string` widget. The `label` for the field can be any string value. See the [Collections reference doc](../configuration-options/#collections) for details on how collections and fields are configured. If you forget to add this field, you will get an error that your collection "must have a field that is a valid entry identifier".
**Note:** Folder collections must have at least one field with the name `title` for creating new entry slugs. That field should use the default `string` widget. The `label` for the field can be any string value. If you wish to use a different field as your identifier, set `identifier_field` to the field name. See the [Collections reference doc](../configuration-options/#collections) for details on how collections and fields are configured. If you forget to add this field, you will get an error that your collection "must have a field that is a valid entry identifier".

Example:

Expand All @@ -30,6 +30,21 @@ collections:
- {label: "Body", name: "body", widget: "markdown"}
```

With `identifier_field`:

```yaml
- label: "Blog"
name: "blog"
folder: "_posts/blog"
create: true
identifier_field: name
fields:
- {label: "Name", name: "name", widget: "string"}
- {label: "Publish Date", name: "date", widget: "datetime"}
- {label: "Featured Image", name: "thumbnail", widget: "image"}
- {label: "Body", name: "body", widget: "markdown"}
```

### Filtered folder collections

The entries for any folder collection can be filtered based on the value of a single field. By filtering a folder into different collections, you can manage files with different fields, options, extensions, etc. in the same folder.
Expand Down