From cdfe1fcb2533959b4e6e5f1738a8109a4fb3e5e7 Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Wed, 13 Jul 2022 11:19:30 +0200 Subject: [PATCH] Convert type customField to underlying data type --- .../server/utils/__tests__/attributes.test.js | 34 +++++++++++++++++++ .../server/utils/attributes.js | 13 +++++++ .../lib/core/registries/custom-fields.js | 3 ++ 3 files changed, 50 insertions(+) create mode 100644 packages/core/content-type-builder/server/utils/__tests__/attributes.test.js diff --git a/packages/core/content-type-builder/server/utils/__tests__/attributes.test.js b/packages/core/content-type-builder/server/utils/__tests__/attributes.test.js new file mode 100644 index 0000000000..66525d33ad --- /dev/null +++ b/packages/core/content-type-builder/server/utils/__tests__/attributes.test.js @@ -0,0 +1,34 @@ +'use strict'; + +const { formatAttribute } = require('../attributes'); + +describe('format attributes', () => { + it('replaces type customField with the underlying data type', () => { + const mockAttribute = { + type: 'customField', + customField: 'plugin::mycustomfields.color', + }; + + global.strapi = { + container: { + // mock container.get('custom-fields') + get: jest.fn(() => ({ + // mock container.get('custom-fields').get(uid) + get: jest.fn(() => ({ + name: 'color', + plugin: 'mycustomfields', + type: 'text', + })), + })), + }, + }; + + const formattedAttribute = formatAttribute('key', mockAttribute); + + const expected = { + type: 'text', + customField: 'plugin::mycustomfields.color', + }; + expect(formattedAttribute).toEqual(expected); + }); +}); diff --git a/packages/core/content-type-builder/server/utils/attributes.js b/packages/core/content-type-builder/server/utils/attributes.js index e90fc1fd1a..980108bd11 100644 --- a/packages/core/content-type-builder/server/utils/attributes.js +++ b/packages/core/content-type-builder/server/utils/attributes.js @@ -67,6 +67,19 @@ const formatAttribute = (key, attribute) => { }; } + if (attribute.type === 'customField') { + const customField = strapi.container.get('custom-fields').get(attribute.customField); + + if (!customField) { + throw new Error(`Could not find Custom Field: ${attribute.customField}`); + } + + return { + ...attribute, + type: customField.type, + }; + } + return attribute; }; diff --git a/packages/core/strapi/lib/core/registries/custom-fields.js b/packages/core/strapi/lib/core/registries/custom-fields.js index db850c8773..2970324556 100644 --- a/packages/core/strapi/lib/core/registries/custom-fields.js +++ b/packages/core/strapi/lib/core/registries/custom-fields.js @@ -10,6 +10,9 @@ const customFieldsRegistry = strapi => { getAll() { return customFields; }, + get(customField) { + return customFields[customField]; + }, add(customField) { const customFieldList = Array.isArray(customField) ? customField : [customField];