mirror of
https://github.com/strapi/strapi.git
synced 2025-11-03 11:25:17 +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);
|
||||
};
|
||||
|
||||
const isMediaAttribute = attr => {
|
||||
return attr.type === 'media';
|
||||
};
|
||||
|
||||
const isMediaAttribute = attribute => attribute.type === 'media';
|
||||
const isRelationalAttribute = attribute => attribute.type === 'relation';
|
||||
const isComponentAttribute = attribute => ['component', 'dynamiczone'].includes(attribute.type);
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ const ctService = require('../../services/content-types')();
|
||||
describe('i18n - Controller - content-types', () => {
|
||||
describe('getNonLocalizedAttributes', () => {
|
||||
beforeEach(() => {
|
||||
const getModel = () => {};
|
||||
const getModel = () => ({});
|
||||
global.strapi = {
|
||||
getModel,
|
||||
plugins: { i18n: { services: { 'content-types': ctService } } },
|
||||
|
||||
@ -20,10 +20,16 @@ module.exports = {
|
||||
|
||||
await validateGetNonLocalizedAttributesInput({ model, id, locale });
|
||||
|
||||
const modelDef = strapi.getModel(model);
|
||||
const { copyNonLocalizedAttributes, isLocalizedContentType } = getService('content-types');
|
||||
const {
|
||||
copyNonLocalizedAttributes,
|
||||
isLocalizedContentType,
|
||||
getNonLocalizedMediaAttributes,
|
||||
} = getService('content-types');
|
||||
const { READ_ACTION, CREATE_ACTION } = strapi.admin.services.constants;
|
||||
|
||||
const modelDef = strapi.getModel(model);
|
||||
const nonLocalizedMediaAttributes = getNonLocalizedMediaAttributes(modelDef);
|
||||
|
||||
if (!isLocalizedContentType(modelDef)) {
|
||||
throw new ApplicationError('model.not.localized');
|
||||
}
|
||||
@ -32,7 +38,7 @@ module.exports = {
|
||||
|
||||
const entity = await strapi
|
||||
.query(model)
|
||||
.findOne({ where: params, populate: ['localizations'] });
|
||||
.findOne({ where: params, populate: [...nonLocalizedMediaAttributes, 'localizations'] });
|
||||
|
||||
if (!entity) {
|
||||
return ctx.notFound();
|
||||
|
||||
@ -82,12 +82,10 @@ const getAndValidateRelatedEntity = async (relatedEntityId, model, locale) => {
|
||||
* @param {*} attribute
|
||||
* @returns
|
||||
*/
|
||||
const isLocalizedAttribute = (model, attributeName) => {
|
||||
const attribute = model.attributes[attributeName];
|
||||
|
||||
const isLocalizedAttribute = attribute => {
|
||||
return (
|
||||
hasLocalizedOption(attribute) ||
|
||||
(isRelationalAttribute(attribute) && !isMediaAttribute(attribute)) ||
|
||||
isRelationalAttribute(attribute) ||
|
||||
isTypedAttribute(attribute, 'uid')
|
||||
);
|
||||
};
|
||||
@ -108,7 +106,20 @@ const isLocalizedContentType = model => {
|
||||
*/
|
||||
const getNonLocalizedAttributes = model => {
|
||||
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[]}
|
||||
*/
|
||||
const getLocalizedAttributes = model => {
|
||||
return getVisibleAttributes(model).filter(attributeName =>
|
||||
isLocalizedAttribute(model, attributeName)
|
||||
return getVisibleAttributes(model).filter(attrName =>
|
||||
isLocalizedAttribute(model.attributes[attrName])
|
||||
);
|
||||
};
|
||||
|
||||
@ -206,4 +217,5 @@ module.exports = () => ({
|
||||
copyNonLocalizedAttributes,
|
||||
getAndValidateRelatedEntity,
|
||||
fillNonLocalizedAttributes,
|
||||
getNonLocalizedMediaAttributes,
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user