diff --git a/src/utilities/getLocalizedFields.spec.ts b/src/utilities/getLocalizedFields.spec.ts index fecb36c..c5a0230 100644 --- a/src/utilities/getLocalizedFields.spec.ts +++ b/src/utilities/getLocalizedFields.spec.ts @@ -880,4 +880,78 @@ describe("fn: getLocalizedFields", () => { }) */ }) + + /** + * @see https://github.com/payloadcms/plugin-seo + * + * payloadcms/plugin-seo adds localized fields. + * If there are no other localized fields, we don't + * want to submit to CrowdIn. + */ + describe("payloadcms/plugin-seo tests", () => { + const seoFields: Field[] = [ + { + "name": "meta", + "label": "SEO", + "type": "group", + "fields": [ + /**{ + "name": "overview", + "label": "Overview", + "type": "ui", + "admin": { + "components": {} + } + },*/ + { + "name": "title", + "type": "text", + "localized": true, + "admin": { + "components": {} + } + }, + { + "name": "description", + "type": "textarea", + "localized": true, + "admin": { + "components": {} + } + }, + /**{ + "name": "preview", + "label": "Preview", + "type": "ui", + "admin": { + "components": {} + } + }**/ + ] + } + ] + + it("excludes payloadcms/plugin-seo localized fields if there are no localized fields on the collection/global", () => { + const nonLocalizedFieldCollection: Field[] = [ + { + name: 'textLocalizedField', + type: 'text', + }, + { + name: 'richTextLocalizedField', + type: 'richText', + }, + { + name: 'textareaLocalizedField', + type: 'text', + }, + ] + + const fields: Field[] = [ + ...nonLocalizedFieldCollection, + ...seoFields, + ] + expect(getLocalizedFields({ fields })).toEqual([]) + }) + }) }) diff --git a/src/utilities/index.ts b/src/utilities/index.ts index 505aecf..0787c31 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -3,7 +3,7 @@ import deepEqual from 'deep-equal' import { FieldWithName } from '../types' import { slateToHtml, payloadSlateToDomConfig } from 'slate-serializers' import type { Descendant } from 'slate' -import { isEmpty, merge } from "lodash" +import { get, isEmpty, merge } from "lodash" const localizedFieldTypes = [ 'richText', @@ -27,6 +27,26 @@ export const getLocalizedFields = ({ fields: Field[], type?: 'json' | 'html', localizedParent?: boolean +}): any[] => { + const localizedFields = getLocalizedFieldsRecursive({ + fields, + type, + localizedParent, + }) + if (localizedFields.length === 1 && get(localizedFields[0], 'name') === 'meta') { + return [] + } + return localizedFields +} + +export const getLocalizedFieldsRecursive = ({ + fields, + type, + localizedParent = false, +}: { + fields: Field[], + type?: 'json' | 'html', + localizedParent?: boolean }): any[] => ([ ...fields // localized or group fields only.