diff --git a/common/src/main/java/org/broadleafcommerce/common/web/dialect/AbstractModelVariableModifierProcessor.java b/common/src/main/java/org/broadleafcommerce/common/web/dialect/AbstractModelVariableModifierProcessor.java index bb707823df..da770ae0bf 100644 --- a/common/src/main/java/org/broadleafcommerce/common/web/dialect/AbstractModelVariableModifierProcessor.java +++ b/common/src/main/java/org/broadleafcommerce/common/web/dialect/AbstractModelVariableModifierProcessor.java @@ -19,6 +19,7 @@ */ package org.broadleafcommerce.common.web.dialect; +import org.broadleafcommerce.common.web.BroadleafRequestContext; import org.thymeleaf.context.ITemplateContext; import org.thymeleaf.model.IProcessableElementTag; import org.thymeleaf.processor.element.AbstractElementTagProcessor; @@ -29,6 +30,8 @@ import java.util.HashSet; import java.util.Set; +import jakarta.servlet.http.HttpServletRequest; + /** * @author apazzolini * @@ -75,6 +78,26 @@ protected void addToModel(IElementTagStructureHandler structureHandler, String k structureHandler.setLocalVariable(key, value); } + /** + * Adds a value to the current request so it is visible to the remainder of the page, including + * sibling elements. {@link #addToModel} contributes a Thymeleaf 3 local variable which is scoped + * to the host element's body; processors whose element is self-closing and whose value is consumed + * by following sibling markup must use this method instead so the value survives outside the body. + * Thymeleaf 3's web context resolves ${var} from the request attributes, so setting the attribute + * on the current request makes the value visible to the rest of the page. Outside of a web request + * there is no request scope, so the value is not added. + * + * @param key the key to add to the request + * @param value the value represented by the key + */ + protected void addToRequest(String key, Object value) { + BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext(); + HttpServletRequest request = (brc != null) ? brc.getRequest() : null; + if (request != null) { + request.setAttribute(key, value); + } + } + @SuppressWarnings("unchecked") protected void addCollectionToExistingSet(ITemplateContext context, IElementTagStructureHandler structureHandler, String key, Collection value) { Set items = (Set) context.getVariable(key); diff --git a/core/broadleaf-framework-web/src/main/java/org/broadleafcommerce/core/web/processor/GoogleAnalyticsProcessor.java b/core/broadleaf-framework-web/src/main/java/org/broadleafcommerce/core/web/processor/GoogleAnalyticsProcessor.java index cb9fc6e0e9..bcc90c67de 100644 --- a/core/broadleaf-framework-web/src/main/java/org/broadleafcommerce/core/web/processor/GoogleAnalyticsProcessor.java +++ b/core/broadleaf-framework-web/src/main/java/org/broadleafcommerce/core/web/processor/GoogleAnalyticsProcessor.java @@ -96,7 +96,9 @@ protected void modifyModelAttributes(ITemplateContext context, IProcessableEleme if (orderNumber != null) { order = orderService.findOrderByOrderNumber(orderNumber); } - addToModel(structureHandler, "analytics", analytics(getWebPropertyId(), order)); + // This element is self-closing and "analytics" is consumed by a sibling