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
Changes from 1 commit
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
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.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
*
Expand All @@ -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'))
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
.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,
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
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'));
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
})
.catch(() => window.eZ.helpers.notification.showErrorNotification('Cannot create content structure'));
};
Expand Down