Skip to content

Commit

Permalink
Implement switchableFieldI18n option for linkSet
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Jun 6, 2024
1 parent 49f5836 commit 1865d08
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package com.merkle.oss.magnolia.definition.custom.linkset;

import com.merkle.oss.magnolia.definition.custom.switchable.AbstractSwitchableDefinitionBuilder;
import com.merkle.oss.magnolia.definition.custom.switchable.FieldOption;
import com.merkle.oss.magnolia.definition.custom.switchable.SwitchableDefinition;
import info.magnolia.ui.editor.JcrChildNodeProviderDefinition;
import info.magnolia.ui.field.NoopNameDecorator;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import com.merkle.oss.magnolia.definition.custom.switchable.AbstractSwitchableDefinitionBuilder;
import com.merkle.oss.magnolia.definition.custom.switchable.FieldOption;
import com.merkle.oss.magnolia.definition.custom.switchable.SwitchableDefinition;

public abstract class AbstractSwitchableLinkSetDefinitionBuilder<B extends AbstractSwitchableLinkSetDefinitionBuilder<B>> extends AbstractSwitchableDefinitionBuilder<LinkType, B> {
public static final String LINK_TYPE_PROPERTY = "link";

@Nullable
private List<LinkType> linkOptions;

protected AbstractSwitchableLinkSetDefinitionBuilder(final String labelPrefix) {
super(labelPrefix);
protected AbstractSwitchableLinkSetDefinitionBuilder(final String labelPrefix, final boolean switchableFieldI18n) {
super(labelPrefix, switchableFieldI18n);
final JcrChildNodeProviderDefinition childNodeProvider = new JcrChildNodeProviderDefinition();
childNodeProvider.setSupportI18N(false);
itemProvider(childNodeProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ public class LinkSetDefinitionBuilder extends AbstractSwitchableLinkSetDefinitio
private final boolean singleTree;

public LinkSetDefinitionBuilder() {
this(false);
this(false, true);
}

protected LinkSetDefinitionBuilder(final boolean singleTree) {
super(LABEL_PREFIX);
protected LinkSetDefinitionBuilder(final boolean singleTree, final boolean switchableFieldI18n) {
super(LABEL_PREFIX, switchableFieldI18n);
this.singleTree = singleTree;
}


@Override
protected FieldOption<LinkType> createFieldOption(final LinkType linkType) {
if(LinkTypes.INTERNAL.equals(linkType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,36 @@ Link to an internal DAM asset.
}
```

## SwitchableFieldI18n
1. Extend LinkSetDefinitionBuilder
```java
import com.merkle.oss.magnolia.definition.custom.linkset.LinkSetDefinitionBuilder;

public class CustomLinkSetDefinitionBuilder extends LinkSetDefinitionBuilder {
public CustomLinkSetDefinitionBuilder() {
super(false, false);
}
}
```
2. Extend LinkModelFactory

```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(
final LocaleProvider localeProvider,
final ExtendedLinkAnchorModifier extendedLinkAnchorModifier,
final Set<LinkType.Resolver> linkTypeResolvers,
final Set<LinkFactory> linkFactories
) {
this(localeProvider, extendedLinkAnchorModifier, linkTypeResolvers, linkFactories, false);
}
}
```

## Custom link-types
1. Define custom link types
```java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,30 @@ public class LinkModelFactory {
private final ExtendedLinkAnchorModifier extendedLinkAnchorModifier;
private final Set<LinkType.Resolver> linkTypeResolvers;
private final Set<LinkFactory> linkFactories;
private final boolean switchableFieldI18n;

@Inject
public LinkModelFactory(
final LocaleProvider localeProvider,
final ExtendedLinkAnchorModifier extendedLinkAnchorModifier,
final Set<LinkType.Resolver> linkTypeResolvers,
final Set<LinkFactory> linkFactories
) {
this(localeProvider, extendedLinkAnchorModifier, linkTypeResolvers, linkFactories, true);
}

protected LinkModelFactory(
final LocaleProvider localeProvider,
final ExtendedLinkAnchorModifier extendedLinkAnchorModifier,
final Set<LinkType.Resolver> linkTypeResolvers,
final Set<LinkFactory> linkFactories,
final boolean switchableFieldI18n
) {
this.localeProvider = localeProvider;
this.extendedLinkAnchorModifier = extendedLinkAnchorModifier;
this.linkTypeResolvers = linkTypeResolvers;
this.linkFactories = linkFactories;
this.switchableFieldI18n = switchableFieldI18n;
}

public Optional<Link> create(final Locale locale, final PowerNode node, final String propertyName) {
Expand All @@ -48,7 +60,7 @@ public Optional<Link> create(final Locale locale, final Locale dialogLocale, fin
return node
.getChild(propertyName)
.flatMap(linkNode ->
linkNode.getProperty(LinkSetDefinitionBuilder.LINK_TYPE_PROPERTY, dialogLocale, ValueConverter::getString).flatMap(this::resolve).flatMap(linkType ->
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)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

public abstract class AbstractSwitchableDefinitionBuilder<T extends OptionEnum, B extends AbstractSwitchableDefinitionBuilder<T, B>> extends AbstractConfiguredComplexPropertyDefinitionBuilder<Node, SwitchableDefinition, B> {
private final String labelPrefix;
private final boolean fieldI18n;

@Nullable
private Class<? extends PropertyNameDecorator> propertyNameDecorator;
Expand All @@ -39,8 +40,9 @@ public abstract class AbstractSwitchableDefinitionBuilder<T extends OptionEnum,
@Nullable
private List<FieldValidatorDefinition> validators;

protected AbstractSwitchableDefinitionBuilder(final String labelPrefix) {
protected AbstractSwitchableDefinitionBuilder(final String labelPrefix, final boolean fieldI18n) {
this.labelPrefix = labelPrefix;
this.fieldI18n = fieldI18n;
}

public B propertyNameDecorator(@Nullable final Class<? extends PropertyNameDecorator> propertyNameDecorator) {
Expand Down Expand Up @@ -121,7 +123,8 @@ public SwitchableDefinition build(final String name) {
Optional.ofNullable(fieldOptions).stream()
.flatMap(Collection::stream)
.map(FieldOption::getField)
.collect(Collectors.toList())
.collect(Collectors.toList()),
fieldI18n
);
super.populate(definition, name);
Optional.ofNullable(readOnly).ifPresent(definition::setReadOnly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ public class SwitchableDefinition extends ConfiguredComplexPropertyDefinition<No
private final Class<? extends PropertyNameDecorator> propertyNameDecoratorClass;
private final AbstractSelectFieldDefinition optionsField; // Can't add generics because magnolia is stripping type info in AbstractOptionGroupFieldDefinition
private final List<SwitchableForm> forms;
private final boolean fieldI18n;
private boolean readOnly;
private boolean required;
private List<FieldValidatorDefinition> validators;

public SwitchableDefinition(
final Class<? extends PropertyNameDecorator> propertyNameDecoratorClass,
final AbstractSelectFieldDefinition optionsField,
final List<SwitchableForm> forms
final List<SwitchableForm> forms,
final boolean fieldI18n
) {
this.propertyNameDecoratorClass = propertyNameDecoratorClass;
this.optionsField = optionsField;
this.forms = forms;
this.fieldI18n = fieldI18n;
setImplementationClass((Class) SwitchableFormView.class);
setItemProvider(new CurrentItemProviderDefinition<>());
}
Expand All @@ -54,8 +57,10 @@ public Class<PropertyNameDecorator> getPropertyNameDecorator() {

@Override
public void setI18n(boolean i18n) {
super.setI18n(i18n);
applyField(field -> field.setI18n(i18n));
if(fieldI18n) {
super.setI18n(i18n);
applyField(field -> field.setI18n(i18n));
}
applyForms(switchableForm -> switchableForm.setI18n(i18n));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public class SwitchableDefinitionBuilder<T extends OptionEnum> extends AbstractSwitchableDefinitionBuilder<T, SwitchableDefinitionBuilder<T>> {
public SwitchableDefinitionBuilder(final String labelPrefix) {
super(labelPrefix);
super(labelPrefix, true);
}
}

0 comments on commit 1865d08

Please sign in to comment.