From e890c992cfd30edbc34bcc50039220318e227854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20D=C4=99bi=C5=84ski?= Date: Mon, 5 Dec 2022 10:54:10 +0100 Subject: [PATCH 1/2] IBX-3814: Added checks if the field to upload is type ezimage to add alternative text --- .../services/multi.file.upload.service.js | 81 +++++++++++++------ 1 file changed, 58 insertions(+), 23 deletions(-) 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..a7d9d3bec6 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.ibexa.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 * @@ -147,29 +171,40 @@ const prepareStruct = ({ parentInfo, config, languageCode }, data) => { 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('Cannot get content type by identifier')) + .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('Cannot create content structure')); }) .catch(() => window.eZ.helpers.notification.showErrorNotification('Cannot create content structure')); }; From f27f4e749c3ded553a4b9c9a8d7397e7112d48b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20D=C4=99bi=C5=84ski?= Date: Tue, 6 Dec 2022 11:36:50 +0100 Subject: [PATCH 2/2] Added translation, Corrected Accept header --- .../translations/multi_file_upload.en.xliff | 10 ++++++++++ .../services/multi.file.upload.service.js | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) 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 a7d9d3bec6..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 @@ -136,7 +136,7 @@ const getFieldDefinitionByIdentifier = ({ token, siteaccess }, contentTypeId, fi const request = new Request(`/api/ezp/v2/content/types/${contentTypeId}/fieldDefinition/${fieldIdentifier}`, { method: 'GET', headers: { - Accept: 'application/vnd.ibexa.api.FieldDefinition+json', + Accept: 'application/vnd.ez.api.FieldDefinition+json', 'X-Siteaccess': siteaccess, 'X-CSRF-Token': token, }, @@ -164,7 +164,7 @@ 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, @@ -176,7 +176,7 @@ const prepareStruct = ({ parentInfo, config, languageCode }, data) => { return getFieldDefinitionByIdentifier(config, contentType.id, contentFieldIdentifier) .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 fieldDefinition = response.FieldDefinition; @@ -192,7 +192,7 @@ const prepareStruct = ({ parentInfo, config, languageCode }, data) => { const struct = { ContentCreate: { ContentType: { _href: contentType._href }, - mainLanguageCode: languageCode || parentInfo.language, + mainLanguageCode: languageCode ?? parentInfo.language, LocationCreate: { ParentLocation: { _href: parentLocation }, sortField: 'PATH', sortOrder: 'ASC' }, Section: null, alwaysAvailable: true, @@ -204,9 +204,9 @@ const prepareStruct = ({ parentInfo, config, languageCode }, data) => { return struct; }) - .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'))); }) - .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'))); }; /**