From a69d22c1d18b00a28d763fef09f86a5313402122 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 17 Dec 2019 21:33:46 -0500 Subject: [PATCH] working localized flexible content, missing autopopulated flexible content --- demo/collections/Page.js | 10 +++++----- src/localization/localization.plugin.js | 10 ++++------ src/mongoose/schema/fieldToSchemaMap.js | 22 +++++++--------------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/demo/collections/Page.js b/demo/collections/Page.js index d6d75ca176..e06a2cbee5 100644 --- a/demo/collections/Page.js +++ b/demo/collections/Page.js @@ -24,7 +24,7 @@ module.exports = { name: 'title', label: 'Page Title', type: 'input', - unique: true, + unique: false, localized: true, maxLength: 100, required: true, @@ -43,7 +43,8 @@ module.exports = { type: 'relationship', relationType: 'reference', relationTo: 'Category', - hasMany: true + hasMany: true, + localized: true, }, { name: 'image', @@ -75,12 +76,11 @@ module.exports = { ] }, { - name: 'flexible', + name: 'blocks', label: 'Flexible Content Blocks', type: 'flexible', - blocks: ['quote', 'cta'], + blocks: ['Quote', 'cta'], localized: true, - hasMany: false, }, { label: 'Meta Information', diff --git a/src/localization/localization.plugin.js b/src/localization/localization.plugin.js index f98f8a32de..c82fef69cc 100644 --- a/src/localization/localization.plugin.js +++ b/src/localization/localization.plugin.js @@ -24,6 +24,7 @@ export default function localizationPlugin(schema, options) { if (path.endsWith('global.en')) { recurse = false; } + if (schemaType.schema) { // propagate plugin initialization for sub-documents schemas schemaType.schema.plugin(localizationPlugin, pluginOptions); if (!recurse) @@ -34,10 +35,6 @@ export default function localizationPlugin(schema, options) { return; } - if (!((schemaType instanceof mongoose.Schema.Types.String) || (schemaType instanceof mongoose.Schema.Types.Embedded))) { - throw new mongoose.Error('localization can only be used on type String or Embedded'); - } - let pathArray = path.split('.'), key = pathArray.pop(), prefix = pathArray.join('.'); @@ -54,7 +51,6 @@ export default function localizationPlugin(schema, options) { }, schema.tree); delete tree[key]; - schema.virtual(path) .get(function () { @@ -89,8 +85,9 @@ export default function localizationPlugin(schema, options) { return value; }) .set(function (value) { + // multiple locales are set as an object - if (typeof value === 'object') { + if (typeof value === 'object' && !Array.isArray(value)) { let locales = this.schema.options.localization.locales; locales.forEach(locale => { if (!value[locale]) { @@ -99,6 +96,7 @@ export default function localizationPlugin(schema, options) { } this.set(`${path}.${locale}`, value[locale]); }, this); + return; } // embedded and sub-documents will use locale methods from the top level document diff --git a/src/mongoose/schema/fieldToSchemaMap.js b/src/mongoose/schema/fieldToSchemaMap.js index 5739f04f14..432d5d801a 100644 --- a/src/mongoose/schema/fieldToSchemaMap.js +++ b/src/mongoose/schema/fieldToSchemaMap.js @@ -63,30 +63,22 @@ const fieldToSchemaMap = { }; }, flexible: (field, options = {}) => { - const flexible = { + const schema = new mongoose.Schema({ value: { - type: mongoose.Types.ObjectId, autopopulate: true, + type: mongoose.Types.ObjectId, refPath: `${options.path ? (options.path + '.') : ''}${field.name}.blockType`, }, blockType: { type: String, enum: field.blocks }, _id: false, - }; + }); - const schema = new mongoose.Schema( - field.hasMany !== false ? [flexible] : flexible, - { - hasMany: field.hasMany, - localized: field.localized || false, - } - ); - - if (field.localized) { - schema.plugin(localizationPlugin, options.localization); - } schema.plugin(autopopulate); - return schema; + return { + localized: field.localized || false, + type: [schema], + } }, };