Skip to content

Commit

Permalink
Fix LinkSet single page (breaking for apps using the single page feat…
Browse files Browse the repository at this point in the history
…ure)

 - internal link text was not i18n
  • Loading branch information
eschleb committed Oct 30, 2024
1 parent 97490a5 commit 94d3839
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,47 +53,60 @@ Link to an internal DAM asset.
<br>
<img alt="jcr-structure" src='assets/type_asset-jcr_structure.png' width='1000'>

## 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
<component>
<type>com.merkle.oss.magnolia.definition.custom.linkset.model.InternalLinkFactory</type>
<implementation>...CustomInternalLinkFactory</implementation>
</component>
```

## 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(
Expand All @@ -105,7 +118,14 @@ Link to an internal DAM asset.
this(localeProvider, extendedLinkAnchorModifier, linkTypeResolvers, linkFactories, false);
}
}
```
```
3. Bind factory
```xml
<component>
<type>com.merkle.oss.magnolia.definition.custom.linkset.model.LinkModelFactory</type>
<implementation>...CustomLinkModelFactory</implementation>
</component>
```

## Custom link-types
1. Define custom link types
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -38,7 +53,7 @@ public boolean test(final LinkType linkType) {

@Override
public Optional<Link> 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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class LinkModelFactory {
private final ExtendedLinkAnchorModifier extendedLinkAnchorModifier;
private final Set<LinkType.Resolver> linkTypeResolvers;
private final Set<LinkFactory> linkFactories;
private final boolean switchableFieldI18n;
private final boolean switchableFieldI18n;

@Inject
public LinkModelFactory(
Expand All @@ -44,7 +44,7 @@ protected LinkModelFactory(
this.extendedLinkAnchorModifier = extendedLinkAnchorModifier;
this.linkTypeResolvers = linkTypeResolvers;
this.linkFactories = linkFactories;
this.switchableFieldI18n = switchableFieldI18n;
this.switchableFieldI18n = switchableFieldI18n;
}

public Optional<Link> create(final Locale locale, final PowerNode node, final String propertyName) {
Expand All @@ -61,7 +61,7 @@ public Optional<Link> 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)
)
);
}
Expand All @@ -72,10 +72,6 @@ private Optional<Link> create(final Locale locale, final Locale dialogLocale, fi
);
}

protected boolean isSingleTree(final LinkType linkType) {
return false;
}

private Optional<LinkType> resolve(final String type) {
return linkTypeResolvers.stream()
.map(resolver -> resolver.resolve(type))
Expand Down

0 comments on commit 94d3839

Please sign in to comment.