Skip to content

Commit

Permalink
Implement singleTree option for linkSet
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Jun 6, 2024
1 parent 12173a8 commit 49f5836
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@

public class BasicLinkSetDefinition<L extends ConfiguredFieldDefinition<?>> extends ConfiguredComplexPropertyDefinition<Node> implements SwitchableForm {
private final L link;
private final boolean singleTree;
@Nullable
private TextFieldDefinition anchorId;
@Nullable
private TextFieldDefinition linkText;
@Nullable
private CheckBoxFieldDefinition openInNewWindow;

public BasicLinkSetDefinition(final L link) {
public BasicLinkSetDefinition(final L link, final boolean singleTree) {
this.link = link;
setImplementationClass((Class) FormView.class);
this.singleTree = singleTree;
setImplementationClass((Class) FormView.class);
setItemProvider(new CurrentItemProviderDefinition<>());
}

Expand Down Expand Up @@ -74,10 +76,12 @@ public FieldLayoutDefinition<?> getLayout() {
@Override
public void setI18n(final boolean i18n) {
super.setI18n(i18n);
link.setI18n(i18n);
getAnchorId().ifPresent(definition ->
definition.setI18n(i18n)
);
if(!singleTree) {
link.setI18n(i18n);
getAnchorId().ifPresent(definition ->
definition.setI18n(i18n)
);
}
getLinkText().ifPresent(definition ->
definition.setI18n(i18n)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class BasicLinkSetDefinitionBuilder<L extends ConfiguredFieldDefinition<?
private Boolean required;
@Nullable
private List<FieldValidatorDefinition> validators;
private boolean singleTree;

public BasicLinkSetDefinitionBuilder<L> singleTree(final boolean singleTree) {
this.singleTree = singleTree;
return self();
}

public BasicLinkSetDefinitionBuilder<L> anchorId(@Nullable final TextFieldDefinition anchorId) {
this.anchorId = anchorId;
Expand Down Expand Up @@ -71,14 +77,14 @@ public BasicLinkSetDefinitionBuilder<L> validators(final List<FieldValidatorDefi
}

public BasicLinkSetDefinition<L> build(final String name, final L link) {
final BasicLinkSetDefinition<L> definition = new BasicLinkSetDefinition<>(link);
super.populate(definition, name);
final BasicLinkSetDefinition<L> definition = new BasicLinkSetDefinition<>(link, singleTree);
Optional.ofNullable(anchorId).ifPresent(definition::setAnchorId);
Optional.ofNullable(linkText).ifPresent(definition::setLinkText);
Optional.ofNullable(openInNewWindow).ifPresent(definition::setOpenInNewWindow);
Optional.ofNullable(readOnly).ifPresent(definition::setReadOnly);
Optional.ofNullable(required).ifPresent(definition::setRequired);
Stream.ofNullable(validators).flatMap(Collection::stream).forEach(definition.getValidators()::add);
super.populate(definition, name);
return definition;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ public class LinkSetDefinitionBuilder extends AbstractSwitchableLinkSetDefinitio
public static final UnaryOperator<String> OPEN_IN_NEW_TAB_PROPERTY_NAME_PROVIDER = name -> name + "_in_new_window";
protected static final String LABEL_PREFIX = "merkle.customDefinitions.linkSet.";
protected static final String FIELD_LABEL_PREFIX = LABEL_PREFIX + "field.";
private final boolean singleTree;

public LinkSetDefinitionBuilder() {
this(false);
}

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

@Override
Expand All @@ -41,6 +47,7 @@ protected FieldOption<LinkType> internal(final LinkType linkType) {
return new FieldOption<>(
linkType,
name -> new BasicLinkSetDefinitionBuilder<>()
.singleTree(singleTree)
.anchorId(anchor(name))
.linkText(linkText(name))
.openInNewWindow(openInNewTab(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ Link to an internal DAM asset.
<br>
<img alt="jcr-structure" src='assets/type_asset-jcr_structure.png' width='1000'>

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

public class CustomLinkSetDefinitionBuilder extends LinkSetDefinitionBuilder {
public CustomLinkSetDefinitionBuilder() {
super(true, 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 {
@Override
protected boolean isSingleTree(final LinkType linkType) {
return LinkTypes.INTERNAL.equals(linkType);
}
}
```

## Custom link-types
1. Define custom link types
```java
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.merkle.oss.magnolia.definition.custom.linkset.model;

import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

import javax.inject.Inject;

import org.apache.commons.lang.NotImplementedException;

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.model.util.ExtendedLinkAnchorModifier;
import com.merkle.oss.magnolia.powernode.PowerNode;
import com.merkle.oss.magnolia.powernode.ValueConverter;
import org.apache.commons.lang.NotImplementedException;

import javax.inject.Inject;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

public class LinkModelFactory {
private final LocaleProvider localeProvider;
Expand Down Expand Up @@ -47,13 +49,21 @@ public Optional<Link> create(final Locale locale, final Locale dialogLocale, fin
.getChild(propertyName)
.flatMap(linkNode ->
linkNode.getProperty(LinkSetDefinitionBuilder.LINK_TYPE_PROPERTY, dialogLocale, ValueConverter::getString).flatMap(this::resolve).flatMap(linkType ->
getLinkFactory(linkType).create(locale, dialogLocale, linkNode, linkType.getValue()).map(extendedLink ->
wrapAnchor(extendedLink, dialogLocale, linkNode, linkType)
)
create(locale, isSingleTree(linkType) ? localeProvider.getDefaultLocale(node) : dialogLocale, linkNode, linkType)
)
);
}

private Optional<Link> create(final Locale locale, final Locale dialogLocale, final PowerNode linkNode, final LinkType linkType) {
return getLinkFactory(linkType).create(locale, dialogLocale, linkNode, linkType.getValue()).map(extendedLink ->
wrapAnchor(extendedLink, dialogLocale, linkNode, linkType)
);
}

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 49f5836

Please sign in to comment.