diff --git a/packages/strapi-plugin-i18n/config/functions/register.js b/packages/strapi-plugin-i18n/config/functions/register.js index d923fec908..46216f6c32 100644 --- a/packages/strapi-plugin-i18n/config/functions/register.js +++ b/packages/strapi-plugin-i18n/config/functions/register.js @@ -1,6 +1,7 @@ 'use strict'; const _ = require('lodash'); +const { PUBLISHED_AT_ATTRIBUTE } = require('strapi-utils').contentTypes.constants; const { getService } = require('../../utils'); @@ -18,7 +19,7 @@ module.exports = () => { configurable: false, visible: false, collection: modelName, - populate: ['id', 'locale', 'published_at'], + populate: ['id', 'locale', PUBLISHED_AT_ATTRIBUTE], }); _.set(attributes, 'locale', { diff --git a/packages/strapi-plugin-i18n/services/__tests__/content-types.test.js b/packages/strapi-plugin-i18n/services/__tests__/content-types.test.js index 4441f88478..1428700910 100644 --- a/packages/strapi-plugin-i18n/services/__tests__/content-types.test.js +++ b/packages/strapi-plugin-i18n/services/__tests__/content-types.test.js @@ -76,6 +76,42 @@ describe('content-types service', () => { }) ).toEqual(['stars', 'price']); }); + + test('Consider locale, localizations & published_at as localized', () => { + expect( + getNonLocalizedAttributes({ + uid: 'test-model', + attributes: { + title: { + type: 'string', + pluginOptions: { + i18n: { + localized: true, + }, + }, + }, + stars: { + type: 'integer', + }, + price: { + type: 'integer', + }, + locale: { + type: 'string', + visible: false, + }, + localizations: { + collection: 'test-model', + visible: false, + }, + published_at: { + type: 'datetime', + visible: false, + }, + }, + }) + ).toEqual(['stars', 'price']); + }); }); describe('getValidLocale', () => { @@ -329,7 +365,7 @@ describe('content-types service', () => { }); describe('copyNonLocalizedAttributes', () => { - test('Does not copy locale & localizations', () => { + test('Does not copy locale, localizations & published_at', () => { const model = { attributes: { title: { @@ -349,9 +385,15 @@ describe('content-types service', () => { }, locale: { type: 'string', + visible: false, }, localizations: { collection: 'test-model', + visible: false, + }, + published_at: { + type: 'datetime', + visible: false, }, }, }; @@ -364,6 +406,7 @@ describe('content-types service', () => { description: 'My super description', locale: 'en', localizations: [1, 2, 3], + published_at: '2021-03-18T09:47:37.557Z', }; const result = copyNonLocalizedAttributes(model, input); diff --git a/packages/strapi-plugin-i18n/services/content-types.js b/packages/strapi-plugin-i18n/services/content-types.js index 771ddd4efe..d174aef646 100644 --- a/packages/strapi-plugin-i18n/services/content-types.js +++ b/packages/strapi-plugin-i18n/services/content-types.js @@ -2,7 +2,7 @@ const { has, prop, isNil } = require('lodash/fp'); const { cloneDeepWith, pick, pipe } = require('lodash/fp'); -const { isRelationalAttribute } = require('strapi-utils').contentTypes; +const { isRelationalAttribute, getVisibleAttributes } = require('strapi-utils').contentTypes; const { getService } = require('../utils'); const isLocalized = modelOrAttribute => { @@ -72,9 +72,9 @@ const isLocalizedAttribute = (model, attributeName) => { * @returns {string[]} */ const getNonLocalizedAttributes = model => { - return Object.keys(model.attributes) - .filter(attributeName => !['locale', 'localizations'].includes(attributeName)) - .filter(attributeName => !isLocalizedAttribute(model, attributeName)); + return getVisibleAttributes(model).filter( + attributeName => !isLocalizedAttribute(model, attributeName) + ); }; const removeIds = cloneDeepWith(value => {