From 3cd0fa0f510dde56eff2a0c332f4c079e98f9eb9 Mon Sep 17 00:00:00 2001 From: Florian Motteau Date: Wed, 13 Mar 2024 18:04:47 +0100 Subject: [PATCH] Fixed: Support "text" attribute on hyperlinks in forms (OFBIZ-12940) "text" attribute on hyperlinks in forms should define the modal title when used in "layered-modal" mode. Code indicates this intention (OfbizUtil.js, HtmlFormMacroLibrary.ftl) but the makeHyperlinkString Freemarker macro and the form renderer don't process this parameter. This commit fixes the modal title behavior in this context. --- .../renderer/macro/MacroFormRenderer.java | 9 +++- .../renderer/macro/MacroFormRendererTest.java | 49 +++++++++++++++++++ .../template/macro/HtmlFormMacroLibrary.ftl | 2 +- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java index 07afa93ebe9..302f81c9578 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java @@ -216,6 +216,7 @@ public void renderHyperlinkField(Appendable writer, Map context, this.request.setAttribute("title", hyperlinkField.getTitle()); this.request.setAttribute("width", hyperlinkField.getWidth()); this.request.setAttribute("height", hyperlinkField.getHeight()); + this.request.setAttribute("text", hyperlinkField.getText(context)); makeHyperlinkByType(writer, hyperlinkField.getLinkType(), modelFormField.getWidgetStyle(), hyperlinkField.getUrlMode(), hyperlinkField.getTarget(context), hyperlinkField.getParameterMap(context, modelFormField.getEntityName(), modelFormField.getServiceName()), hyperlinkField.getDescription(context), hyperlinkField.getTargetWindow(context), @@ -2296,6 +2297,7 @@ public void makeHyperlinkString(Appendable writer, String linkStyle, String targ String width = ""; String height = ""; String title = ""; + String text = ""; String hiddenFormName = WidgetWorker.makeLinkHiddenFormName(context, modelFormField); if (UtilValidate.isNotEmpty(modelFormField.getEvent()) && UtilValidate.isNotEmpty(modelFormField.getAction(context))) { event = modelFormField.getEvent(); @@ -2324,6 +2326,9 @@ public void makeHyperlinkString(Appendable writer, String linkStyle, String targ if (UtilValidate.isNotEmpty(request.getAttribute("id"))) { id = request.getAttribute("id").toString(); } + if (UtilValidate.isNotEmpty(request.getAttribute("text"))) { + text = request.getAttribute("text").toString(); + } if (UtilValidate.isNotEmpty(request.getAttribute("uniqueItemName"))) { uniqueItemName = request.getAttribute("uniqueItemName").toString(); width = request.getAttribute("width").toString(); @@ -2370,7 +2375,7 @@ public void makeHyperlinkString(Appendable writer, String linkStyle, String targ sr.append(targetWindow); sr.append("\" description=\""); sr.append(description); - sr.append("\" confirmation =\""); + sr.append("\" confirmation=\""); sr.append(confirmation); sr.append("\" uniqueItemName=\""); sr.append(uniqueItemName); @@ -2380,6 +2385,8 @@ public void makeHyperlinkString(Appendable writer, String linkStyle, String targ sr.append(width); sr.append("\" id=\""); sr.append(id); + sr.append("\" text=\""); + sr.append(text); sr.append("\" />"); executeMacro(writer, sr.toString()); } diff --git a/framework/widget/src/test/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRendererTest.java b/framework/widget/src/test/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRendererTest.java index 2b22108a600..3964d961ce6 100644 --- a/framework/widget/src/test/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRendererTest.java +++ b/framework/widget/src/test/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRendererTest.java @@ -43,6 +43,7 @@ import org.apache.ofbiz.base.util.UtilProperties; import org.apache.ofbiz.base.util.template.FreeMarkerWorker; import org.apache.ofbiz.entity.Delegator; +import org.apache.ofbiz.webapp.control.ConfigXMLReader; import org.apache.ofbiz.webapp.control.RequestHandler; import org.apache.ofbiz.widget.model.FieldInfo; import org.apache.ofbiz.widget.model.ModelForm; @@ -921,6 +922,54 @@ public void hyperlinkFieldMacroRenderedTruncatedWithTitle(@Mocked ModelFormField assertAndGetMacroString("makeHyperlinkString", ImmutableMap.of("description", "DESCR…", "title", description)); } + @Test + public void hyperlinkFieldMacroRenderedModalParameters(@Mocked ModelFormField.HyperlinkField hyperlinkField) throws IOException { + final String title = "TitleValue"; + final String text = "TextValue"; + final String description = "DescriptionValue"; + final String target = "Encoded Target"; + final String id = "IdValue"; + final String uniqueItemName = "UniqueItemName"; + final String width = "650"; + final String height = "150"; + final String confirmation = "Are you sure ?"; + final String targetWindow = "_blank"; + final Map requestMapMap = new HashMap<>(); + final Map parameterMap = new HashMap<>(); + parameterMap.put("k1", "v1"); + parameterMap.put("k2", "v2"); + + new Expectations() { + { + hyperlinkField.getDescription(withNotNull()); result = description; + hyperlinkField.getTarget(withNotNull()); result = target; + hyperlinkField.getParameterMap(withNotNull(), withNull(), withNull()); result = parameterMap; + hyperlinkField.getConfirmation(withNotNull()); result = confirmation; + hyperlinkField.getTargetWindow(withNotNull()); result = targetWindow; + request.getAttribute("title"); result = title; + request.getAttribute("text"); result = text; + request.getAttribute("requestMapMap"); result = requestMapMap; + request.getAttribute("id"); result = id; + request.getAttribute("uniqueItemName"); result = uniqueItemName; + request.getAttribute("width"); result = width; + request.getAttribute("height"); result = height; + } + }; + + macroFormRenderer.renderHyperlinkField(appendable, new HashMap<>(), hyperlinkField); + ImmutableMap result = ImmutableMap.builder() + .put("title", title) + .put("description", description) + .put("linkUrl", "Encoded%20Target") + .put("id", id) + .put("targetParameters", "{'k1':'v1','k2':'v2'}") + .put("width", width) + .put("confirmation", confirmation) + .put("targetWindow", targetWindow) + .build(); + assertAndGetMacroString("makeHyperlinkString", result); + } + private String assertAndGetMacroString(final String expectedName) { return assertAndGetMacroString(expectedName, ImmutableMap.of()); } diff --git a/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl b/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl index 13e4b54ad7e..154f67c3f29 100644 --- a/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl +++ b/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl @@ -836,7 +836,7 @@ Parameter: delegatorName, String, optional - name of the delegator in context. <#if confirmation?has_content> onclick="return confirm('${confirmation?js_string}')"> <#if imgSrc?has_content>${description} -<#macro makeHyperlinkString hiddenFormName imgSrc imgTitle title alternate linkUrl description linkStyle="" event="" action="" targetParameters="" targetWindow="" confirmation="" uniqueItemName="" height="" width="" id=""> +<#macro makeHyperlinkString hiddenFormName imgSrc imgTitle title alternate linkUrl description text="" linkStyle="" event="" action="" targetParameters="" targetWindow="" confirmation="" uniqueItemName="" height="" width="" id=""> <#if uniqueItemName?has_content> <#local params = "{"presentation": "layer""> <#if targetParameters?has_content && !targetParameters?is_hash>