diff --git a/src/bundle/Resources/translations/multi_file_upload.en.xliff b/src/bundle/Resources/translations/multi_file_upload.en.xliff index 545820c165..7129ad90e2 100644 --- a/src/bundle/Resources/translations/multi_file_upload.en.xliff +++ b/src/bundle/Resources/translations/multi_file_upload.en.xliff @@ -71,6 +71,16 @@ Multi-file upload key: upload_popup.close + + Cannot get content type by identifier + Cannot get content type by identifier + key: cannot_get_content_type.identifier + + + Cannot create content structure + Cannot create content structure + key: cannot_get_content_type.identifier + diff --git a/src/bundle/ui-dev/src/modules/multi-file-upload/services/multi.file.upload.service.js b/src/bundle/ui-dev/src/modules/multi-file-upload/services/multi.file.upload.service.js index 6e23a79d81..d5deb08141 100644 --- a/src/bundle/ui-dev/src/modules/multi-file-upload/services/multi.file.upload.service.js +++ b/src/bundle/ui-dev/src/modules/multi-file-upload/services/multi.file.upload.service.js @@ -123,6 +123,30 @@ const getContentTypeByIdentifier = ({ token, siteaccess }, identifier) => { return fetch(request).then(handleRequestResponse); }; +/** + * Get content type field definition by identifier + * + * @function getFieldDefinitionByIdentifier + * @param {Object} params params object containing token and siteaccess properties + * @param {Int} contentTypeId content type id + * @param {String} fieldIdentifier content type field identifier + * @returns {Promise} + */ +const getFieldDefinitionByIdentifier = ({ token, siteaccess }, contentTypeId, fieldIdentifier) => { + const request = new Request(`/api/ezp/v2/content/types/${contentTypeId}/fieldDefinition/${fieldIdentifier}`, { + method: 'GET', + headers: { + Accept: 'application/vnd.ez.api.FieldDefinition+json', + 'X-Siteaccess': siteaccess, + 'X-CSRF-Token': token, + }, + credentials: 'same-origin', + mode: 'cors', + }); + + return fetch(request).then(handleRequestResponse); +}; + /** * Prepares a ContentCreate struct based on an uploaded file type * @@ -140,38 +164,49 @@ const prepareStruct = ({ parentInfo, config, languageCode }, data) => { return getContentTypeByIdentifier(config, mapping.contentTypeIdentifier) .then((response) => response.json()) - .catch(() => window.eZ.helpers.notification.showErrorNotification('Cannot get content type by identifier')) + .catch(() => window.eZ.helpers.notification.showErrorNotification(Translator.trans(/*@Desc("Cannot get content type by identifier")*/ 'cannot_get_content_type_identifier.message', {}, 'multi_file_upload'))) .then((response) => { const fileValue = { fileName: data.file.name, data: data.fileReader.result.replace(/^.*;base64,/, ''), }; - if (data.file.type.startsWith('image/')) { - fileValue.alternativeText = data.file.name; - } - - const fields = [ - { fieldDefinitionIdentifier: mapping.nameFieldIdentifier, fieldValue: data.file.name }, - { fieldDefinitionIdentifier: mapping.contentFieldIdentifier, fieldValue: fileValue }, - ]; - - const struct = { - ContentCreate: { - ContentType: { _href: response.ContentTypeInfoList.ContentType[0]._href }, - mainLanguageCode: languageCode || parentInfo.language, - LocationCreate: { ParentLocation: { _href: parentLocation }, sortField: 'PATH', sortOrder: 'ASC' }, - Section: null, - alwaysAvailable: true, - remoteId: null, - modificationDate: new Date().toISOString(), - fields: { field: fields }, - }, - }; - - return struct; + const contentType = response.ContentTypeInfoList.ContentType[0]; + const contentFieldIdentifier = mapping.contentFieldIdentifier; + + return getFieldDefinitionByIdentifier(config, contentType.id, contentFieldIdentifier) + .then((response) => response.json()) + .catch(() => window.eZ.helpers.notification.showErrorNotification(Translator.trans(/*@Desc("Cannot get content type by identifier")*/ 'cannot_get_content_type_identifier.message', {}, 'multi_file_upload'))) + .then((response) => { + const fieldDefinition = response.FieldDefinition; + + if (fieldDefinition.fieldType === 'ezimage') { + fileValue.alternativeText = data.file.name; + } + + const fields = [ + { fieldDefinitionIdentifier: mapping.nameFieldIdentifier, fieldValue: data.file.name }, + { fieldDefinitionIdentifier: contentFieldIdentifier, fieldValue: fileValue }, + ]; + + const struct = { + ContentCreate: { + ContentType: { _href: contentType._href }, + mainLanguageCode: languageCode ?? parentInfo.language, + LocationCreate: { ParentLocation: { _href: parentLocation }, sortField: 'PATH', sortOrder: 'ASC' }, + Section: null, + alwaysAvailable: true, + remoteId: null, + modificationDate: new Date().toISOString(), + fields: { field: fields }, + }, + }; + + return struct; + }) + .catch(() => window.eZ.helpers.notification.showErrorNotification(Translator.trans(/*@Desc("Cannot create content structure")*/ 'cannot_create_content_structure.message', {}, 'multi_file_upload'))); }) - .catch(() => window.eZ.helpers.notification.showErrorNotification('Cannot create content structure')); + .catch(() => window.eZ.helpers.notification.showErrorNotification(Translator.trans(/*@Desc("Cannot create content structure")*/ 'cannot_create_content_structure.message', {}, 'multi_file_upload'))); }; /**