mirror of
https://github.com/strapi/strapi.git
synced 2025-11-12 16:22:10 +00:00
add non-localized media in /get-non-localized-fields response
This commit is contained in:
parent
6343ce509c
commit
d581c43349
@ -107,10 +107,7 @@ const isScalarAttribute = attribute => {
|
|||||||
return !['component', 'relation', 'dynamiczone'].includes(attribute.type);
|
return !['component', 'relation', 'dynamiczone'].includes(attribute.type);
|
||||||
};
|
};
|
||||||
|
|
||||||
const isMediaAttribute = attr => {
|
const isMediaAttribute = attribute => attribute.type === 'media';
|
||||||
return attr.type === 'media';
|
|
||||||
};
|
|
||||||
|
|
||||||
const isRelationalAttribute = attribute => attribute.type === 'relation';
|
const isRelationalAttribute = attribute => attribute.type === 'relation';
|
||||||
const isComponentAttribute = attribute => ['component', 'dynamiczone'].includes(attribute.type);
|
const isComponentAttribute = attribute => ['component', 'dynamiczone'].includes(attribute.type);
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ const ctService = require('../../services/content-types')();
|
|||||||
describe('i18n - Controller - content-types', () => {
|
describe('i18n - Controller - content-types', () => {
|
||||||
describe('getNonLocalizedAttributes', () => {
|
describe('getNonLocalizedAttributes', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const getModel = () => {};
|
const getModel = () => ({});
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
getModel,
|
getModel,
|
||||||
plugins: { i18n: { services: { 'content-types': ctService } } },
|
plugins: { i18n: { services: { 'content-types': ctService } } },
|
||||||
|
|||||||
@ -20,10 +20,16 @@ module.exports = {
|
|||||||
|
|
||||||
await validateGetNonLocalizedAttributesInput({ model, id, locale });
|
await validateGetNonLocalizedAttributesInput({ model, id, locale });
|
||||||
|
|
||||||
const modelDef = strapi.getModel(model);
|
const {
|
||||||
const { copyNonLocalizedAttributes, isLocalizedContentType } = getService('content-types');
|
copyNonLocalizedAttributes,
|
||||||
|
isLocalizedContentType,
|
||||||
|
getNonLocalizedMediaAttributes,
|
||||||
|
} = getService('content-types');
|
||||||
const { READ_ACTION, CREATE_ACTION } = strapi.admin.services.constants;
|
const { READ_ACTION, CREATE_ACTION } = strapi.admin.services.constants;
|
||||||
|
|
||||||
|
const modelDef = strapi.getModel(model);
|
||||||
|
const nonLocalizedMediaAttributes = getNonLocalizedMediaAttributes(modelDef);
|
||||||
|
|
||||||
if (!isLocalizedContentType(modelDef)) {
|
if (!isLocalizedContentType(modelDef)) {
|
||||||
throw new ApplicationError('model.not.localized');
|
throw new ApplicationError('model.not.localized');
|
||||||
}
|
}
|
||||||
@ -32,7 +38,7 @@ module.exports = {
|
|||||||
|
|
||||||
const entity = await strapi
|
const entity = await strapi
|
||||||
.query(model)
|
.query(model)
|
||||||
.findOne({ where: params, populate: ['localizations'] });
|
.findOne({ where: params, populate: [...nonLocalizedMediaAttributes, 'localizations'] });
|
||||||
|
|
||||||
if (!entity) {
|
if (!entity) {
|
||||||
return ctx.notFound();
|
return ctx.notFound();
|
||||||
|
|||||||
@ -82,12 +82,10 @@ const getAndValidateRelatedEntity = async (relatedEntityId, model, locale) => {
|
|||||||
* @param {*} attribute
|
* @param {*} attribute
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const isLocalizedAttribute = (model, attributeName) => {
|
const isLocalizedAttribute = attribute => {
|
||||||
const attribute = model.attributes[attributeName];
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
hasLocalizedOption(attribute) ||
|
hasLocalizedOption(attribute) ||
|
||||||
(isRelationalAttribute(attribute) && !isMediaAttribute(attribute)) ||
|
isRelationalAttribute(attribute) ||
|
||||||
isTypedAttribute(attribute, 'uid')
|
isTypedAttribute(attribute, 'uid')
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -108,7 +106,20 @@ const isLocalizedContentType = model => {
|
|||||||
*/
|
*/
|
||||||
const getNonLocalizedAttributes = model => {
|
const getNonLocalizedAttributes = model => {
|
||||||
return getVisibleAttributes(model).filter(
|
return getVisibleAttributes(model).filter(
|
||||||
attributeName => !isLocalizedAttribute(model, attributeName)
|
attrName => !isLocalizedAttribute(model.attributes[attrName])
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the list of media attribute names that are not localized
|
||||||
|
* @param {object} model
|
||||||
|
* @returns {string[]}
|
||||||
|
*/
|
||||||
|
const getNonLocalizedMediaAttributes = model => {
|
||||||
|
return getVisibleAttributes(model).filter(
|
||||||
|
attrName =>
|
||||||
|
isMediaAttribute(model.attributes[attrName]) &&
|
||||||
|
!isLocalizedAttribute(model.attributes[attrName])
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -170,8 +181,8 @@ const copyNonLocalizedAttributes = (model, entry) => {
|
|||||||
* @returns {string[]}
|
* @returns {string[]}
|
||||||
*/
|
*/
|
||||||
const getLocalizedAttributes = model => {
|
const getLocalizedAttributes = model => {
|
||||||
return getVisibleAttributes(model).filter(attributeName =>
|
return getVisibleAttributes(model).filter(attrName =>
|
||||||
isLocalizedAttribute(model, attributeName)
|
isLocalizedAttribute(model.attributes[attrName])
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -206,4 +217,5 @@ module.exports = () => ({
|
|||||||
copyNonLocalizedAttributes,
|
copyNonLocalizedAttributes,
|
||||||
getAndValidateRelatedEntity,
|
getAndValidateRelatedEntity,
|
||||||
fillNonLocalizedAttributes,
|
fillNonLocalizedAttributes,
|
||||||
|
getNonLocalizedMediaAttributes,
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user