Skip to content

Commit

Permalink
IBX-3814: Added checks if the field to upload is type ezimage to add …
Browse files Browse the repository at this point in the history
…alternative text (#2082)

* IBX-3814: Added checks if the field to upload is type ezimage to add alternative text

* Added translation, Corrected Accept header

Co-authored-by: Mateusz Dębiński <[email protected]>
  • Loading branch information
mateuszdebinski and Mateusz Dębiński authored Dec 14, 2022
1 parent 94b3384 commit 5a0421a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
10 changes: 10 additions & 0 deletions src/bundle/Resources/translations/multi_file_upload.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@
<target state="new">Multi-file upload</target>
<note>key: upload_popup.close</note>
</trans-unit>
<trans-unit id="6b2bc429ffc374b49b5de9e3563d3d5a7668e4b2" resname="cannot_get_content_type_identifier.message">
<source>Cannot get content type by identifier</source>
<target state="new">Cannot get content type by identifier</target>
<note>key: cannot_get_content_type.identifier</note>
</trans-unit>
<trans-unit id="6b2bc429ffc374b49b5de9e3563d3d5a7668e4b2" resname="cannot_create_content_structure.message">
<source>Cannot create content structure</source>
<target state="new">Cannot create content structure</target>
<note>key: cannot_get_content_type.identifier</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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')));
};

/**
Expand Down

0 comments on commit 5a0421a

Please sign in to comment.