Skip to content

Commit

Permalink
add multi-content support for files-based collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Piérre Reimertz committed Jul 24, 2020
1 parent c92be7c commit 8ebe03a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
21 changes: 20 additions & 1 deletion packages/netlify-cms-core/src/actions/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,39 @@ export function applyDefaults(config) {

const files = collection.get('files');
if (files) {
const isI18n = !isEmpty(locales) && collection.get('i18n_structure');
const identifier_field = selectIdentifier(collection);

collection = collection.delete('nested');
collection = collection.delete('meta');
collection = collection.set(
'files',
files.map(file => {
const fields = file.get('fields');

file = file.set('file', trimStart(file.get('file'), '/'));
file = setDefaultPublicFolder(file);
file = file.set(
'fields',
traverseFields(file.get('fields'), setDefaultPublicFolder),
isI18n
? addLocaleFields(traverseFields(fields, setDefaultPublicFolder), locales)
: traverseFields(fields, setDefaultPublicFolder),
);

return file;
}),
);
if (!isEmpty(locales) && collection.get('i18n_structure')) {
if (!collection.get('default_locale')) {
collection = collection.set('default_locale', locales[0]);
} else {
locales = uniq([collection.get('default_locale'), ...locales]);
}
// add identifier config
collection = collection.set('identifier_field', `${locales[0]}.${identifier_field}`);

collection = collection.set('locales', fromJS(locales));
}
}

if (!collection.has('sortableFields')) {
Expand Down
35 changes: 26 additions & 9 deletions packages/netlify-cms-core/src/reducers/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,32 @@ export const duplicateFields = (collection: Collection, values: any) => {
const data = values.toJS();
const locales = collection.get('locales') as List<string>;
const defaultLocale = collection.get('default_locale') as string;
const paths = selectDuplicateFieldPaths(values, List([collection.get('fields').first()]));
paths.forEach((path: string) => {
const duplicateValue = get(data, path);
if (duplicateValue) {
locales.shift().forEach(l => {
set(data, `${l}${path.substring(defaultLocale.length)}`, duplicateValue);
});
}
});

if (collection.get('type') === FOLDER) {
const paths = selectDuplicateFieldPaths(values, List([collection.get('fields').first()]));
paths.forEach((path: string) => {
const duplicateValue = get(data, path);
if (duplicateValue) {
locales.shift().forEach(l => {
set(data, `${l}${path.substring(defaultLocale.length)}`, duplicateValue);
});
}
});
} else {
collection.get('files')?.forEach(file => {
if (file) {
const paths = selectDuplicateFieldPaths(values, List([file.get('fields').first()]));
paths.forEach((path: string) => {
const duplicateValue = get(data, path);
if (duplicateValue) {
locales.shift().forEach(l => {
set(data, `${l}${path.substring(defaultLocale.length)}`, duplicateValue);
});
}
});
}
});
}

return fromJS(data);
};
Expand Down

0 comments on commit 8ebe03a

Please sign in to comment.