diff --git a/api/src/services/fields.ts b/api/src/services/fields.ts index 08c4b685e5..0342b0c31b 100644 --- a/api/src/services/fields.ts +++ b/api/src/services/fields.ts @@ -77,7 +77,7 @@ export class FieldsService { let aliasFields = await aliasQuery; - const aliasTypes = ['alias', 'o2m', 'm2m', 'translations']; + const aliasTypes = ['alias', 'o2m', 'm2m', 'files', 'files', 'translations']; aliasFields = aliasFields.filter((field) => { const specials = (field.special || '').split(','); diff --git a/app/src/interfaces/translations/translations.vue b/app/src/interfaces/translations/translations.vue index aafc414a2f..ae397188da 100644 --- a/app/src/interfaces/translations/translations.vue +++ b/app/src/interfaces/translations/translations.vue @@ -68,7 +68,13 @@ export default defineComponent({ const fieldsStore = useFieldsStore(); const relationsStore = useRelationsStore(); - const { relations, translationsCollection, languagesCollection, languageField, translationsPrimaryKeyField } = useRelation(); + const { + relations, + translationsCollection, + languagesCollection, + languageField, + translationsPrimaryKeyField, + } = useRelation(); const { languages, @@ -84,7 +90,7 @@ export default defineComponent({ const { info, primaryKeyField } = useCollection(languagesCollection); const defaultTemplate = info.value?.meta?.display_template; - return defaultTemplate || props.template || `{{ $${primaryKeyField.value.field} }}`; + return props.template || defaultTemplate || `{{ ${primaryKeyField.value.field} }}`; }); return { @@ -113,10 +119,12 @@ export default defineComponent({ const translationsRelation = computed(() => { if (!relations.value || relations.value.length === 0) return null; - return relations.value.find((relation: Relation) => { - return relation.one_collection === props.collection && relation.one_field === props.field; - }) || null; - }) + return ( + relations.value.find((relation: Relation) => { + return relation.one_collection === props.collection && relation.one_field === props.field; + }) || null + ); + }); const translationsCollection = computed(() => { if (!translationsRelation.value) return null; @@ -130,9 +138,11 @@ export default defineComponent({ const languagesRelation = computed(() => { if (!relations.value || relations.value.length === 0) return null; - return relations.value.find((relation: Relation) => { - return relation.one_collection !== props.collection && relation.one_field !== props.field; - }) || null; + return ( + relations.value.find((relation: Relation) => { + return relation.one_collection !== props.collection && relation.one_field !== props.field; + }) || null + ); }); const languagesCollection = computed(() => { @@ -143,9 +153,15 @@ export default defineComponent({ const languageField = computed(() => { if (!languagesRelation.value) return null; return languagesRelation.value.many_field; - }) + }); - return { relations, translationsCollection, languagesCollection, languageField, translationsPrimaryKeyField }; + return { + relations, + translationsCollection, + languagesCollection, + languageField, + translationsPrimaryKeyField, + }; } function useLanguages() { diff --git a/app/src/modules/settings/routes/data-model/field-detail/field-detail.vue b/app/src/modules/settings/routes/data-model/field-detail/field-detail.vue index d09d18066a..51f1a94fa0 100644 --- a/app/src/modules/settings/routes/data-model/field-detail/field-detail.vue +++ b/app/src/modules/settings/routes/data-model/field-detail/field-detail.vue @@ -287,6 +287,13 @@ export default defineComponent({ }) ); + await Promise.all( + Object.keys(state.newRows).map((collection) => { + const rows = state.newRows[collection]; + return api.post(`/items/${collection}`, rows); + }) + ); + await collectionsStore.hydrate(); await fieldsStore.hydrate(); await relationsStore.hydrate(); diff --git a/app/src/modules/settings/routes/data-model/field-detail/store.ts b/app/src/modules/settings/routes/data-model/field-detail/store.ts index 7c191d9932..d208311055 100644 --- a/app/src/modules/settings/routes/data-model/field-detail/store.ts +++ b/app/src/modules/settings/routes/data-model/field-detail/store.ts @@ -56,6 +56,7 @@ function initLocalStore( newCollections: [], newFields: [], updateFields: [], + newRows: {}, autoFillJunctionRelation: true, }); @@ -390,22 +391,96 @@ function initLocalStore( } if (collectionExists(relatedCollection) === false) { - state.newCollections.push({ - $type: 'related', - collection: relatedCollection, - fields: [ - { - field: state.relations[1].one_primary, - type: 'integer', - schema: { - has_auto_increment: true, + if (type === 'translations') { + state.newCollections.push({ + $type: 'related', + collection: relatedCollection, + fields: [ + { + field: state.relations[1].one_primary, + type: 'string', + schema: { + is_primary_key: true, + }, + meta: { + interface: 'text-input', + options: { + iconLeft: 'vpn_key', + }, + width: 'half', + }, }, - meta: { - hidden: true, + { + field: 'name', + type: 'string', + schema: {}, + meta: { + interface: 'text-input', + options: { + iconLeft: 'translate', + }, + width: 'half', + }, }, - }, - ], - }); + ], + }); + } else { + state.newCollections.push({ + $type: 'related', + collection: relatedCollection, + fields: [ + { + field: state.relations[1].one_primary, + type: 'integer', + schema: { + has_auto_increment: true, + }, + meta: { + hidden: true, + }, + }, + ], + }); + } + } + + if (type === 'translations') { + if (collectionExists(relatedCollection) === false) { + state.newRows = { + [relatedCollection]: [ + { + code: 'en-US', + name: 'English', + }, + { + code: 'de-DE', + name: 'German', + }, + { + code: 'fr-Fr', + name: 'French', + }, + { + code: 'ru-RU', + name: 'Russian', + }, + { + code: 'es-ES', + name: 'Spanish', + }, + { + code: 'it-IT', + name: 'Italian', + }, + { + code: 'pt-BR', + name: 'Portuguese', + }, + ], + }; + } else { + state.newRows = {}; + } } }, 50); @@ -542,15 +617,17 @@ function initLocalStore( ); state.relations[0].many_collection = `${collection}_translations`; + state.relations[0].many_field = `${collection}_${ fieldsStore.getPrimaryKeyFieldForCollection(collection)?.field }`; + state.relations[1].one_collection = 'languages'; if (collectionExists('languages')) { state.relations[1].one_primary = fieldsStore.getPrimaryKeyFieldForCollection('languages')?.field; } else { - state.relations[1].one_primary = 'id'; + state.relations[1].one_primary = 'code'; } state.relations[1].many_field = `${state.relations[1].one_collection}_${state.relations[1].one_primary}`;