Skip to content

Commit

Permalink
BACKLOG-21361: Replaced most of systemName adapter by override
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Draier committed Aug 3, 2023
1 parent c8d4d4b commit 50b7e08
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 146 deletions.
35 changes: 2 additions & 33 deletions src/javascript/ContentEditor/ContentEditor/adaptSystemNameField.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Constants} from '~/ContentEditor/ContentEditor.constants';
import {decodeSystemName} from '~/ContentEditor/utils';

const isContentOrFileNode = formData => {
const pattern = '^/sites/[^/]*/(contents|files)$';
Expand All @@ -10,47 +9,17 @@ const isContentOrFileNode = formData => {
};

// TODO completely get rid of this adapter

Check warning on line 11 in src/javascript/ContentEditor/ContentEditor/adaptSystemNameField.js

View workflow job for this annotation

GitHub Actions / Static Analysis (linting, vulns)

Unexpected 'todo' comment: 'TODO completely get rid of this adapter'
export const adaptSystemNameField = (rawData, formData, t, primaryNodeType, isCreate, readOnlyByMixin) => {
export const adaptSystemNameField = (formData, primaryNodeType) => {
const systemNameField = formData.sections
.flatMap(section => section.fieldSets)
.flatMap(fieldSet => fieldSet.fields)
.find(field => field.name.endsWith('_' + Constants.systemName.propertyName));

if (systemNameField) {
systemNameField.name = Constants.systemName.name;

// Add i18ns label to field / should be in json field definition
systemNameField.displayName = t('jcontent:label.contentEditor.section.fieldSet.system.fields.systemName');

// Add description to the field / should be in json field definition, with specific overrides per "read only mixin"
// Parameterized resource bundles not supported yet
systemNameField.description = readOnlyByMixin ?
t('jcontent:label.contentEditor.section.fieldSet.system.fields.systemNameDescriptionReadOnly') :
t('jcontent:label.contentEditor.section.fieldSet.system.fields.systemNameDescription', {maxNameSize: window.contextJsParameters.config.maxNameSize});

// Add max name size validation / should be in json field definition
systemNameField.selectorOptions = [
{
name: 'maxLength',
value: window.contextJsParameters.config.maxNameSize
}
];

// System name should be readonly for this specific nodetypes / should be in json overrides
if (readOnlyByMixin ||
Constants.systemName.READONLY_FOR_NODE_TYPES.includes(primaryNodeType.name) ||
isContentOrFileNode(formData) ||
(!isCreate && !formData.nodeData.hasWritePermission) ||
formData.nodeData.lockedAndCannotBeEdited) {
if (Constants.systemName.READONLY_FOR_NODE_TYPES.includes(primaryNodeType.name) || isContentOrFileNode(formData)) {
systemNameField.readOnly = true;
}
}

// Set initial value for system name / should be move to getInitialValues
if (isCreate) {
formData.initialValues[Constants.systemName.name] = rawData.jcr.result.newName;
} else {
formData.initialValues[Constants.systemName.name] = decodeSystemName(rawData.jcr.result.name);
}
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {useFormDefinition} from '~/ContentEditor/ContentEditor/useFormDefinition
const getInitialValues = (sections, nodeData) => {
// Work in progress default value
const wipInfo = {[Constants.wip.fieldName]: {status: nodeData.defaultWipInfo.status, languages: nodeData.defaultWipInfo.languages}};

const name = {[Constants.systemName.name]: nodeData.newName};

// Retrieve fields and the return object contains the field name as the key and the field value as the value
return {...getFields(sections).reduce((result, field) => ({...result, ...getFieldValuesFromDefaultValues(field)}), {}), ...wipInfo};
return {...getFields(sections).reduce((result, field) => ({...result, ...getFieldValuesFromDefaultValues(field)}), {}), ...wipInfo, ...name};
};

/**
Expand Down Expand Up @@ -41,7 +44,7 @@ export const adaptCreateFormData = (data, lang, t, contentEditorConfigContext) =
nodeTypeName: data.jcr.nodeTypeByName.name
};

adaptSystemNameField(data, formData, t, data.jcr.nodeTypeByName, true);
adaptSystemNameField(formData, data.jcr.nodeTypeByName);

if (contentEditorConfigContext.name) {
formData.initialValues[Constants.systemName.name] = contentEditorConfigContext.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {adaptSections, getExpandedSections} from '~/ContentEditor/ContentEditor/
import {getFieldValuesFromDefaultValues} from '~/ContentEditor/ContentEditor/getFieldValuesFromDefaultValues';
import {EditFormQuery} from '~/ContentEditor/ContentEditor/edit.gql-queries';
import {useFormDefinition} from '~/ContentEditor/ContentEditor/useFormDefinitions';
import {decodeSystemName} from '../utils';

// TODO https://jira.jahia.org/browse/TECH-300

Check warning on line 12 in src/javascript/ContentEditor/ContentEditor/useEditFormDefinition.js

View workflow job for this annotation

GitHub Actions / Static Analysis (linting, vulns)

Unexpected 'todo' comment: 'TODO...'

Expand All @@ -31,8 +32,10 @@ const getInitialValues = (nodeData, sections) => {
wipInfo[Constants.wip.fieldName] = {status: nodeData.defaultWipInfo.status, languages: nodeData.defaultWipInfo.languages};
}

const name = {[Constants.systemName.name]: decodeSystemName(nodeData.name)};

// Return object contains fields and dynamic fieldSets
return {...nodeValues, ...extendsMixinFieldsDefaultValues, ...dynamicFieldSets, ...childrenOrderingFields, ...wipInfo};
return {...nodeValues, ...extendsMixinFieldsDefaultValues, ...dynamicFieldSets, ...childrenOrderingFields, ...wipInfo, ...name};
};

const getChildrenOrderingFields = (nodeData, dynamicFieldSets) => {
Expand Down Expand Up @@ -162,7 +165,7 @@ export const adaptEditFormData = (data, lang, t) => {
nodeTypeName: nodeData.primaryNodeType.name
};

adaptSystemNameField(data, formData, t, nodeData.primaryNodeType, false, nodeData.isSystemNameReadOnlyMixin);
adaptSystemNameField(formData, nodeData.primaryNodeType);

return formData;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jahia.services.content.nodetypes.initializers.ChoiceListInitializer;
import org.jahia.services.content.nodetypes.initializers.ChoiceListInitializerService;
import org.jahia.services.content.nodetypes.initializers.ChoiceListValue;
import org.jahia.settings.SettingsBean;
import org.jahia.utils.i18n.Messages;
import org.osgi.framework.Bundle;
import org.osgi.service.component.annotations.Component;
Expand Down Expand Up @@ -196,6 +197,11 @@ private Form getEditorForm(ExtendedNodeType primaryNodeType, JCRNodeWrapper exis
boolean forceReadOnly = field.getExtendedPropertyDefinition() != null && field.getExtendedPropertyDefinition().isInternationalized() ? !i18nFieldsEditable : !sharedFieldsEditable;
field.setReadOnly((field.isReadOnly() != null && field.isReadOnly()) || forceReadOnly);

if (field.getSelectorOptionsMap() != null && field.getSelectorOptionsMap().size() > 0) {
field.setSelectorOptionsMap(field.getSelectorOptionsMap().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> SettingsBean.getInstance().replaceBySubsitutor((String) entry.getValue()))));
}

if (field.getValueConstraints() != null && field.getExtendedPropertyDefinition() != null) {
List<FieldValueConstraint> valueConstraints = getValueConstraints(primaryNodeType, field, existingNode, parentNode, locale, new HashMap<>());
if (valueConstraints != null && !valueConstraints.isEmpty()) {
Expand Down Expand Up @@ -245,25 +251,24 @@ public static String resolveResourceKey(String key, Locale locale, JCRSiteNode s
return key;
}
logger.debug("Resources key: {}", key);
String baseName = null;
String value;
if (key.contains("@")) {
if (key.contains("resources.")) {
baseName = StringUtils.substringAfter(key, "@");
key = StringUtils.substringBefore(key, "@");
} else {
baseName = StringUtils.substringBefore(key, "@");
key = StringUtils.substringAfter(key, "@");
}
String osgiBundleName = null;

if (key.contains(":")) {
osgiBundleName = StringUtils.substringBefore(key, ":");
key = StringUtils.substringAfter(key, ":");
}

value = Messages.get(baseName, site != null ? site.getTemplatePackage() : null, key, locale, StringUtils.EMPTY);
if (StringUtils.isEmpty(value) && baseName != null) {
JahiaTemplatesPackage jahiaTemplatesPackage = ServicesRegistry.getInstance().getJahiaTemplateManagerService().getTemplatePackageRegistry().lookupById(baseName);
value = Messages.get(baseName, jahiaTemplatesPackage, key, locale, StringUtils.EMPTY);
} else if (value == null) {
value = Messages.getInternal(key, locale);
// First lookup in site templates (with dependencies and internal resources)
String value = Messages.get(null, site.getTemplatePackage(), key, locale, StringUtils.EMPTY);

if (StringUtils.isEmpty(value) && osgiBundleName != null) {
// Then look in module resources (with dependencies and internal resources)
JahiaTemplatesPackage jahiaTemplatesPackage = ServicesRegistry.getInstance().getJahiaTemplateManagerService().getTemplatePackageRegistry().lookupById(osgiBundleName);
value = Messages.get(null, jahiaTemplatesPackage, key, locale, StringUtils.EMPTY);
}
// Replace placeholders in labels
value = SettingsBean.getInstance().replaceBySubsitutor(value);

return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static Field generateEditorFormField(ExtendedItemDefinition itemDefinitio

String errorMessageKey = itemDefinition.getResourceBundleKey() + ".constraint.error.message";
if (itemDefinition.getDeclaringNodeType().getTemplatePackage() != null) {
errorMessageKey += "@" + itemDefinition.getDeclaringNodeType().getTemplatePackage().getResourceBundleName();
errorMessageKey = itemDefinition.getDeclaringNodeType().getTemplatePackage().getName() + ":" + errorMessageKey;
}

String selectorType = SelectorType.nameFromValue(propertyDefinition.getSelector());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
package org.jahia.modules.contenteditor.api.forms;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.jahia.modules.contenteditor.api.forms.model.Field;
import org.jahia.modules.contenteditor.api.forms.model.FieldSet;
import org.jahia.modules.contenteditor.api.forms.model.Form;
import org.jahia.modules.contenteditor.api.forms.model.Section;
import org.jahia.modules.contenteditor.api.forms.model.*;
import org.jahia.modules.contenteditor.utils.ContentEditorUtils;
import org.jahia.services.content.nodetypes.ExtendedNodeType;
import org.jahia.services.content.nodetypes.NodeTypeRegistry;
import org.jetbrains.annotations.Nullable;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
Expand Down Expand Up @@ -139,8 +138,14 @@ public void registerForm(URL editorFormURL, Bundle bundle) {
for (FieldSet fieldSet : section.getFieldSets()) {
initFieldSet(fieldSet, bundle);
}

section.setLabelKey(ContentEditorUtils.getLabelKey(section.getLabelKey(), bundle));
section.setDescriptionKey(ContentEditorUtils.getLabelKey(section.getDescriptionKey(), bundle));
}

form.setLabelKey(ContentEditorUtils.getLabelKey(form.getLabelKey(), bundle));
form.setDescriptionKey(ContentEditorUtils.getLabelKey(form.getDescriptionKey(), bundle));

forms.add(form);
formsByBundle.computeIfAbsent(bundle, b -> new ArrayList<>()).add(form);
logger.info("Successfully loaded static form for name {} from {}", form.getNodeType().getName(), editorFormURL);
Expand Down Expand Up @@ -196,6 +201,10 @@ private static void initFieldSet(FieldSet fieldSet, Bundle originBundle) {
if (fieldSet.getPriority() == null) {
fieldSet.setPriority(1.);
}

fieldSet.setLabelKey(ContentEditorUtils.getLabelKey(fieldSet.getLabelKey(), originBundle));
fieldSet.setDescriptionKey(ContentEditorUtils.getLabelKey(fieldSet.getDescriptionKey(), originBundle));

for (Field field : fieldSet.getFields()) {
try {
if (field.getDeclaringNodeType() != null) {
Expand All @@ -205,8 +214,17 @@ private static void initFieldSet(FieldSet fieldSet, Bundle originBundle) {
} catch (NoSuchNodeTypeException e) {
throw new RuntimeException(e);
}

field.setLabelKey(ContentEditorUtils.getLabelKey(field.getLabelKey(), originBundle));
field.setDescriptionKey(ContentEditorUtils.getLabelKey(field.getDescriptionKey(), originBundle));
field.setErrorMessageKey(ContentEditorUtils.getLabelKey(field.getErrorMessageKey(), originBundle));

if (field.getValueConstraints() != null) {
for (FieldValueConstraint valueConstraint : field.getValueConstraints()) {
valueConstraint.setDisplayValueKey(ContentEditorUtils.getLabelKey(valueConstraint.getDisplayValueKey(), originBundle));
}
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ public void initializeLabel(Locale uiLocale, JCRSiteNode site, ExtendedNodeType
}

private void initializeLabelFromItemDefinition(ExtendedItemDefinition definition, Locale uiLocale, JCRSiteNode site, ExtendedNodeType nodeType) {
String suffix = nodeType.getTemplatePackage() != null ? "@" + nodeType.getTemplatePackage().getResourceBundleName() : "";
String prefix = nodeType.getTemplatePackage() != null ? nodeType.getTemplatePackage().getBundle().getSymbolicName() + ":" : "";
String key = definition.getResourceBundleKey(nodeType);
label = StringUtils.isEmpty(label) ? StringEscapeUtils.unescapeHtml(resolveResourceKey(key + suffix, uiLocale, site)) : label;
description = StringUtils.isEmpty(description) ? Sanitizers.FORMATTING.sanitize(resolveResourceKey(key + ".ui.tooltip" + suffix, uiLocale, site)) : description;
errorMessage = StringUtils.isEmpty(errorMessage)? Sanitizers.FORMATTING.sanitize(resolveResourceKey(key + ".constraint.error.message" + suffix, uiLocale, site)) : errorMessage;
label = StringUtils.isEmpty(label) ? StringEscapeUtils.unescapeHtml(resolveResourceKey(prefix + key, uiLocale, site)) : label;
description = StringUtils.isEmpty(description) ? Sanitizers.FORMATTING.sanitize(resolveResourceKey(prefix + key + ".ui.tooltip", uiLocale, site)) : description;
errorMessage = StringUtils.isEmpty(errorMessage)? Sanitizers.FORMATTING.sanitize(resolveResourceKey(prefix + key + ".constraint.error.message", uiLocale, site)) : errorMessage;
}

public void mergeWith(Field otherField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ public void initializeLabel(Locale uiLocale, JCRSiteNode site) {
description = description == null && descriptionKey != null ? resolveResourceKey(descriptionKey, uiLocale, site) : description;

if (nodeType != null) {
String suffix = nodeType.getTemplatePackage() != null ? "@" + nodeType.getTemplatePackage().getResourceBundleName() : "";
String prefix = nodeType.getTemplatePackage() != null ? nodeType.getTemplatePackage().getBundle().getSymbolicName() + ":" : "";
String key = JCRContentUtils.replaceColon(nodeType.getName());
label = StringUtils.isEmpty(label) ? StringEscapeUtils.unescapeHtml(resolveResourceKey(key + suffix, uiLocale, site)) : label;
description = StringUtils.isEmpty(description) ? Sanitizers.FORMATTING.sanitize(resolveResourceKey(key + ".ui.tooltip" + suffix, uiLocale, site)) : description;
label = StringUtils.isEmpty(label) ? StringEscapeUtils.unescapeHtml(resolveResourceKey(prefix + key, uiLocale, site)) : label;
description = StringUtils.isEmpty(description) ? Sanitizers.FORMATTING.sanitize(resolveResourceKey(prefix + key + ".ui.tooltip", uiLocale, site)) : description;

label = StringUtils.isEmpty(label) ? StringUtils.substringAfter(nodeType.getName(), ":") : label;
description = StringUtils.isEmpty(description) ? Sanitizers.FORMATTING.sanitize(resolveResourceKey(key + "_description" + suffix, uiLocale, site)) : description;
description = StringUtils.isEmpty(description) ? Sanitizers.FORMATTING.sanitize(resolveResourceKey(prefix + key + "_description", uiLocale, site)) : description;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jahia.services.content.nodetypes.ExtendedNodeType;
import org.jahia.services.content.nodetypes.NodeTypeRegistry;
import org.jahia.utils.LanguageCodeConverters;
import org.osgi.framework.Bundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.touk.throwing.ThrowingConsumer;
Expand Down Expand Up @@ -177,4 +178,13 @@ public static JCRNodeWrapper resolveNodeFromPathorUUID(String uuidOrPath, Locale
}
}

public static String getLabelKey(String key, Bundle originBundle) {
if (!StringUtils.isEmpty(key) && !key.contains(":") && originBundle != null) {
return originBundle.getSymbolicName() + ":" + key;
}

return key;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"fields": [
{
"name": "ce:systemName",
"descriptionKey": "jcontent:label.contentEditor.section.fieldSet.system.fields.systemNameDescriptionReadOnly",
"readOnly": true
}
]
Expand Down
Loading

0 comments on commit 50b7e08

Please sign in to comment.