Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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