From 94d38395d2c726a4a71abda7c288337689d9964c Mon Sep 17 00:00:00 2001 From: eschleb Date: Wed, 30 Oct 2024 11:39:26 +0100 Subject: [PATCH] Fix LinkSet single page (breaking for apps using the single page feature) - internal link text was not i18n --- .../definition/custom/linkset/README.md | 80 ++++++++++++------- .../linkset/model/InternalLinkFactory.java | 23 +++++- .../linkset/model/LinkModelFactory.java | 10 +-- 3 files changed, 72 insertions(+), 41 deletions(-) diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md index 90d7a1e..2e77732 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md @@ -53,47 +53,60 @@ Link to an internal DAM asset.
jcr-structure -## SingleTree +## SingleTree / SwitchableFieldI18n 1. Extend LinkSetDefinitionBuilder -```java - import com.merkle.oss.magnolia.definition.custom.linkset.LinkSetDefinitionBuilder; - - public class CustomLinkSetDefinitionBuilder extends LinkSetDefinitionBuilder { - public CustomLinkSetDefinitionBuilder() { - super(true, false); + ```java + import com.merkle.oss.magnolia.definition.custom.linkset.LinkSetDefinitionBuilder; + + public class CustomLinkSetDefinitionBuilder extends LinkSetDefinitionBuilder { + public CustomLinkSetDefinitionBuilder() { + super(true, false); + } } - } -``` -2. Extend LinkModelFactory -```java + ``` +2. Extend InternalLinkFactory + ```java + import com.merkle.oss.magnolia.definition.custom.configuration.LinkUtil; + import com.merkle.oss.magnolia.definition.custom.configuration.LocaleProvider; import com.merkle.oss.magnolia.definition.custom.linkset.LinkTypes; - import com.merkle.oss.magnolia.definition.custom.linkset.model.LinkModelFactory; - - public class CustomLinkModelFactory extends LinkModelFactory { - @Override - protected boolean isSingleTree(final LinkType linkType) { - return LinkTypes.INTERNAL.equals(linkType); + import com.merkle.oss.magnolia.definition.custom.linkset.model.InternalLinkFactory; + import com.merkle.oss.magnolia.powernode.PowerNodeService; + + public class CustomInternalLinkFactory extends InternalLinkFactory { + @Inject + public LinkModelFactory( + final PowerNodeService powerNodeService, + final LinkUtil linkUtil, + final LocaleProvider localeProvider + ) { + this(powerNodeService, linkUtil, localeProvider, true); } } -``` + ``` +3. Bind factory + ```xml + + com.merkle.oss.magnolia.definition.custom.linkset.model.InternalLinkFactory + ...CustomInternalLinkFactory + + ``` ## SwitchableFieldI18n 1. Extend LinkSetDefinitionBuilder -```java - import com.merkle.oss.magnolia.definition.custom.linkset.LinkSetDefinitionBuilder; - - public class CustomLinkSetDefinitionBuilder extends LinkSetDefinitionBuilder { - public CustomLinkSetDefinitionBuilder() { - super(false, false); + ```java + import com.merkle.oss.magnolia.definition.custom.linkset.LinkSetDefinitionBuilder; + + public class CustomLinkSetDefinitionBuilder extends LinkSetDefinitionBuilder { + public CustomLinkSetDefinitionBuilder() { + super(false, false); + } } - } -``` + ``` 2. Extend LinkModelFactory - -```java + ```java import com.merkle.oss.magnolia.definition.custom.linkset.LinkTypes; import com.merkle.oss.magnolia.definition.custom.linkset.model.LinkModelFactory; - + public class CustomLinkModelFactory extends LinkModelFactory { @Inject public LinkModelFactory( @@ -105,7 +118,14 @@ Link to an internal DAM asset. this(localeProvider, extendedLinkAnchorModifier, linkTypeResolvers, linkFactories, false); } } -``` + ``` +3. Bind factory + ```xml + + com.merkle.oss.magnolia.definition.custom.linkset.model.LinkModelFactory + ...CustomLinkModelFactory + + ``` ## Custom link-types 1. Define custom link types diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/InternalLinkFactory.java b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/InternalLinkFactory.java index ec07539..4b3e039 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/InternalLinkFactory.java +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/InternalLinkFactory.java @@ -1,6 +1,7 @@ package com.merkle.oss.magnolia.definition.custom.linkset.model; import com.merkle.oss.magnolia.definition.custom.configuration.LinkUtil; +import com.merkle.oss.magnolia.definition.custom.configuration.LocaleProvider; import com.merkle.oss.magnolia.definition.custom.linkset.LinkSetDefinitionBuilder; import com.merkle.oss.magnolia.definition.custom.linkset.LinkType; import com.merkle.oss.magnolia.definition.custom.linkset.LinkTypes; @@ -21,15 +22,29 @@ public class InternalLinkFactory implements LinkModelFactory.LinkFactory { private final PowerNodeService powerNodeService; private final LinkUtil linkUtil; + private final LocaleProvider localeProvider; + private final boolean singleTree; - @Inject + @Inject public InternalLinkFactory( final PowerNodeService powerNodeService, - final LinkUtil linkUtil + final LinkUtil linkUtil, + final LocaleProvider localeProvider + ) { + this(powerNodeService, linkUtil, localeProvider, false); + } + + protected InternalLinkFactory( + final PowerNodeService powerNodeService, + final LinkUtil linkUtil, + final LocaleProvider localeProvider, + final boolean singleTree ) { this.powerNodeService = powerNodeService; this.linkUtil = linkUtil; - } + this.localeProvider = localeProvider; + this.singleTree = singleTree; + } @Override public boolean test(final LinkType linkType) { @@ -38,7 +53,7 @@ public boolean test(final LinkType linkType) { @Override public Optional create(final Locale locale, final Locale dialogLocale, final PowerNode node, final String name) { - return node.getProperty(name, dialogLocale, ValueConverter::getString) + return node.getProperty(name, singleTree ? localeProvider.getDefaultLocale(node) : dialogLocale, ValueConverter::getString) .flatMap(identifier -> powerNodeService.getByIdentifier(WEBSITE, identifier) ) diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/LinkModelFactory.java b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/LinkModelFactory.java index e27a917..46090a2 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/LinkModelFactory.java +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/LinkModelFactory.java @@ -21,7 +21,7 @@ public class LinkModelFactory { private final ExtendedLinkAnchorModifier extendedLinkAnchorModifier; private final Set linkTypeResolvers; private final Set linkFactories; - private final boolean switchableFieldI18n; + private final boolean switchableFieldI18n; @Inject public LinkModelFactory( @@ -44,7 +44,7 @@ protected LinkModelFactory( this.extendedLinkAnchorModifier = extendedLinkAnchorModifier; this.linkTypeResolvers = linkTypeResolvers; this.linkFactories = linkFactories; - this.switchableFieldI18n = switchableFieldI18n; + this.switchableFieldI18n = switchableFieldI18n; } public Optional create(final Locale locale, final PowerNode node, final String propertyName) { @@ -61,7 +61,7 @@ public Optional create(final Locale locale, final Locale dialogLocale, fin .getChild(propertyName) .flatMap(linkNode -> linkNode.getProperty(LinkSetDefinitionBuilder.LINK_TYPE_PROPERTY, switchableFieldI18n ? dialogLocale : localeProvider.getDefaultLocale(node), ValueConverter::getString).flatMap(this::resolve).flatMap(linkType -> - create(locale, isSingleTree(linkType) ? localeProvider.getDefaultLocale(node) : dialogLocale, linkNode, linkType) + create(locale, dialogLocale, linkNode, linkType) ) ); } @@ -72,10 +72,6 @@ private Optional create(final Locale locale, final Locale dialogLocale, fi ); } - protected boolean isSingleTree(final LinkType linkType) { - return false; - } - private Optional resolve(final String type) { return linkTypeResolvers.stream() .map(resolver -> resolver.resolve(type))