Skip to content

Commit

Permalink
Replace supplier page (node property) with templateNode param
Browse files Browse the repository at this point in the history
 - cleanup CmsAreaTemplateHelper
  • Loading branch information
eschleb committed Jan 18, 2024
1 parent 796cbb8 commit dfd5dc0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 71 deletions.
13 changes: 6 additions & 7 deletions Helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ The result on the page is an area bar and an end marker. The title property is r
```
Parameters:

| Key |Type |Description
|----------|-----------|-----------
| name | String |Name of the area element
| area | String |Name of the area definition node such as ContentArea, Footer or Stage.
| editable | boolean |By setting the value to false you can prevent area editing.
| Key | Type |Description
|--------------|---------|-----------
| name | String |Name of the area element
| area | String |Name of the area definition node such as ContentArea, Footer or Stage.
| editable | boolean |By setting the value to false you can prevent area editing.
| templateNode | Node |Node containing the area

Additional key-value params are added to the context. Can be used to pass key-value pairs to components inside this area.
#### supplier-page
The 'mgnl:supplierPage' node property can be set to reference another page which contains the desired area-node.

### [cms-component](light-development/src/main/java/com/merkle/oss/magnolia/renderer/handlebars/helpers/magnolia/CmsComponentTemplateHelper.java)
The cms-component helper renders a component. This tag is commonly used inside the area hbs file referenced by the cms-area helper.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.Set;

public class CmsAreaTemplateHelper extends AbstractCmsTemplateHelper<Object> {
public static final String SUPPLIER_PAGE_PROPERTY = "mgnl:supplierPage";
private final TemplateDefinitionRegistry templateDefinitionRegistry;
private final RenderingEngine renderingEngine;
private final AreaCreationListener areaCreationListener;
Expand Down Expand Up @@ -65,24 +64,24 @@ public Optional<CharSequence> applySafe(final Object ignored, final Options opti
final RenderingModel<?> model = getRenderingModel(options.context).orElseThrow(() ->
new IllegalArgumentException("Rendering model not present!")
);
final Node node = getSupplierPage(model.getNode()).orElseGet(model::getNode);
final Node templateNode = Optional.ofNullable((Node)options.hash("templateNode")).orElseGet(model::getNode);

final AreaState areaState = getOrCreateAreaState(area, node);
final Node areaStateNode = areaState.getNode();
final String workspace = areaStateNode.getSession().getWorkspace().getName();
final String nodeIdentifier = areaStateNode.getIdentifier();
final String path = areaStateNode.getPath();
final AreaDefinition areaDefinition = getAreaDefinition(name, templateNode);
final Node areaNode = getOrCreateAreaNode(name, templateNode, areaDefinition);
final String workspace = areaNode.getSession().getWorkspace().getName();
final String nodeIdentifier = areaNode.getIdentifier();
final String path = areaNode.getPath();

//Magnolia's AreaElement.begin calls render with empty HashMap --> use ContextObjectsRenderingEngineWrapper
final AreaElement areaElement = Components.getComponentProvider().newInstance(
CustomAvailableComponentsAreaElement.class,
new ContextObjectsRenderingEngineWrapper(renderingEngine, options.hash)
);
areaElement.setContent(areaStateNode);
areaElement.setContent(areaNode);
areaElement.setWorkspace(workspace);
areaElement.setNodeIdentifier(nodeIdentifier);
areaElement.setPath(path);
areaElement.setArea(areaState.getAreaDefinition());
areaElement.setArea(areaDefinition);
areaElement.setName(name);
areaElement.setContextAttributes(options.hash);

Expand All @@ -107,8 +106,8 @@ private Optional<RenderingModel> getRenderingModel(final Context context) {
}

private AreaDefinition getAreaDefinition(final String name, final Node node) throws RenderException {
return getNearestAncestorTemplate(node)
.flatMap(this::getTemplateDefinition)
final String templateId = PropertyUtil.getString(node, NodeTypes.Renderable.TEMPLATE);
return getTemplateDefinition(templateId)
.flatMap(templateDefinition ->
getAreaDefinition(name, templateDefinition.getAreas())
)
Expand All @@ -117,12 +116,6 @@ private AreaDefinition getAreaDefinition(final String name, final Node node) thr
);
}

private Optional<String> getNearestAncestorTemplate(final Node node) {
return Optional
.ofNullable(PropertyUtil.getString(node, NodeTypes.Renderable.TEMPLATE))
.or(() -> getParent(node).flatMap(this::getNearestAncestorTemplate));
}

private Optional<TemplateDefinition> getTemplateDefinition(final String templateId) {
try {
return Optional.of(templateDefinitionRegistry.getProvider(templateId).get());
Expand Down Expand Up @@ -153,25 +146,13 @@ private Optional<AreaDefinition> getAreaDefinition(final String name, final Map<
.findFirst();
}

private AreaState getOrCreateAreaState(final String name, final Node node) throws RenderException {
final AreaDefinition areaDefinition = getAreaDefinition(name, node);
if(Boolean.FALSE.equals(areaDefinition.getCreateAreaNode())) {
return new AreaState(areaDefinition, node);
}
return getOrCreateAreaNode(name, node, areaDefinition)
.map(areaNode ->
new AreaState(areaDefinition, areaNode)
)
.orElseThrow(() ->
new RenderException("Couldn't create areaState. " + name + " " + NodeUtil.getPathIfPossible(node))
);
}

private Optional<Node> getOrCreateAreaNode(final String name, final Node node, final AreaDefinition areaDefinition) {
private Node getOrCreateAreaNode(final String name, final Node node, final AreaDefinition areaDefinition) throws RenderException {
try {
return Optional.of(node.getNode(name));
return node.getNode(name);
} catch (PathNotFoundException e) {
return createAreaNode(name, node, areaDefinition);
return createAreaNode(name, node, areaDefinition).orElseThrow(() ->
new RenderException("Couldn't create areaState. " + name + " " + NodeUtil.getPathIfPossible(node))
);
} catch (RepositoryException e) {
throw Exceptions.sneak().handle(e);
}
Expand All @@ -190,18 +171,6 @@ private Optional<Node> createAreaNode(final String name, final Node node, final
});
}

private Optional<Node> getSupplierPage(final Node node) {
return Optional.ofNullable(PropertyUtil.getString(node, SUPPLIER_PAGE_PROPERTY)).flatMap(supplierPageId -> {
try {
return Optional.of(node.getSession().getNodeByIdentifier(supplierPageId));
} catch (ItemNotFoundException e) {
return Optional.empty();
} catch (RepositoryException e) {
throw Exceptions.sneak().handle(e);
}
});
}

@Override
public Set<String> names() {
return Set.of(
Expand All @@ -210,24 +179,6 @@ public Set<String> names() {
);
}

public static final class AreaState {
private final AreaDefinition areaDefinition;
private final Node node;

private AreaState(final AreaDefinition areaDefinition, final Node node) {
this.areaDefinition = areaDefinition;
this.node = node;
}

public AreaDefinition getAreaDefinition() {
return areaDefinition;
}

public Node getNode() {
return node;
}
}

public static class CustomAvailableComponentsAreaElement extends AreaElement {
private final AvailableComponentsResolver availableComponentsResolver;

Expand Down

0 comments on commit dfd5dc0

Please sign in to comment.