Commit 4edbba2f authored by Jeroen Hoffman's avatar Jeroen Hoffman

CMS-11137 [Back port to 11.2] CMS-10840 in FieldPluginHelper, support for...

CMS-11137 [Back port to 11.2] CMS-10840 in FieldPluginHelper, support for getting translated captions for plugins that do not have a field configured, from a 'captionKey' configuration.

Also support a default caption as method parameter, for backwards compatiblity, and use that from the MixinPlugin.

(cherry picked from commit af13829f)
parent 86a74852
......@@ -70,35 +70,43 @@ public class FieldPluginHelper implements IDetachable {
final String typeName = config.getString(AbstractFieldPlugin.TYPE);
final String fieldName = config.getString(AbstractFieldPlugin.FIELD);
if (fieldName == null) {
log.error("No field was specified in the configuration");
} else {
final ITemplateEngine engine = getTemplateEngine();
if (engine != null) {
try {
if (typeName == null) {
documentType = engine.getType(getNodeModel());
} else {
documentType = engine.getType(typeName);
}
field = documentType.getField(fieldName);
if (field == null) {
log.warn("Could not find field with name {} in {}; has the field been added " +
"without committing the document type?", fieldName, documentType.getName());
} else if ("*".equals(field.getPath())) {
log.warn("Field path * is not supported, field name is {} in document type",
fieldName, documentType.getName());
field = null;
} else if (field.getPath() == null) {
log.error("No path available for field {}", fieldName);
field = null;
}
} catch (TemplateEngineException tee) {
log.error("Could not resolve field for name " + fieldName, tee);
}
final ITemplateEngine engine = getTemplateEngine();
if (engine == null) {
log.error("No template engine available for type {}, field {}, config {}", typeName, fieldName, config);
return;
}
try {
if (typeName == null) {
documentType = engine.getType(getNodeModel());
} else {
log.error("No template engine available for {}", fieldName);
documentType = engine.getType(typeName);
}
if (documentType == null) {
log.warn("No documentType found for type name {} or node model {}", typeName, getNodeModel());
}
else if (fieldName == null) {
log.debug("No field was specified for type {} in the configuration {}", documentType.getName(), config);
}
else {
field = documentType.getField(fieldName);
if (field == null) {
log.warn("Could not find field with name {} in type {}; has the field been added " +
"without committing the document type?", fieldName, documentType.getName());
} else if ("*".equals(field.getPath())) {
log.warn("Field path * is not supported, field name is {} in document type {}",
fieldName, documentType.getName());
field = null;
} else if (field.getPath() == null) {
log.error("No path available for field {}", fieldName);
field = null;
}
}
} catch (TemplateEngineException tee) {
log.error("Could not resolve field for name " + fieldName, tee);
}
// FIXME: don't validate in "view" mode?
......@@ -154,20 +162,49 @@ public class FieldPluginHelper implements IDetachable {
}
public IModel<String> getCaptionModel(final Component component) {
return this.getCaptionModel(component, null);
}
public IModel<String> getCaptionModel(final Component component, final String defaultCaption) {
String caption = (defaultCaption != null) ? defaultCaption : getPluginConfig().getString("caption");
String captionKey;
final IFieldDescriptor field = getField();
if (field == null) {
return Model.of("undefined");
if (field != null) {
captionKey = field.getPath();
final String translation = getStringFromBundle(captionKey);
if (translation != null) {
return Model.of(translation);
}
captionKey = field.getName();
if (caption == null && !captionKey.isEmpty()) {
caption = StringUtils.capitalize(captionKey);
}
}
String captionKey = field.getPath();
final String translation = getStringFromBundle(captionKey);
if (translation != null) {
return Model.of(translation);
else {
final String key = getPluginConfig().getString("captionKey");
if ((key != null) && !key.isEmpty()) {
captionKey = key;
final String translation = getStringFromBundle(captionKey);
if (translation != null) {
return Model.of(translation);
}
if (caption == null) {
caption = StringUtils.capitalize(captionKey);
}
}
else {
captionKey = StringUtils.lowerCase(caption);
}
}
String caption = getPluginConfig().getString("caption");
captionKey = field.getName();
if (caption == null && !field.getName().isEmpty()) {
caption = StringUtils.capitalize(field.getName());
// safety, may still occur
if (captionKey == null) {
return Model.of("undefined");
}
// this should be replaced by
// return caption
// after deprecation period of translator method
......@@ -211,8 +248,7 @@ public class FieldPluginHelper implements IDetachable {
}
private String getBundleName() {
final String docType = getDocumentType().getName();
return HIPPO_TYPES + "." + docType;
return (documentType == null) ? null : HIPPO_TYPES + "." + documentType.getName();
}
}
/*
* Copyright 2008-2017 Hippo B.V. (http://www.onehippo.com)
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
......@@ -34,6 +34,7 @@ import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RefreshingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.hippoecm.frontend.editor.ITemplateEngine;
import org.hippoecm.frontend.editor.TemplateEngineException;
import org.hippoecm.frontend.editor.plugins.field.FieldPluginHelper;
......@@ -50,6 +51,9 @@ import org.hippoecm.frontend.types.ITypeDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Plugin that renders a list of configured mixin types in the document editor as checkboxes.
*/
public class MixinPlugin extends RenderPlugin {
private static final long serialVersionUID = 1L;
......@@ -73,7 +77,8 @@ public class MixinPlugin extends RenderPlugin {
}
helper = new FieldPluginHelper(context, config);
add(new Label("name", helper.getCaptionModel(this)));
final String defaultCaption = new StringResourceModel("mixins", this, null).getString();
add(new Label("name", helper.getCaptionModel(this, defaultCaption)));
ITemplateEngine engine = context.getService(config.getString(ITemplateEngine.ENGINE), ITemplateEngine.class);
......@@ -235,4 +240,4 @@ public class MixinPlugin extends RenderPlugin {
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment