Skip to content

Commit

Permalink
Implement ChildNodeWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Apr 18, 2024
1 parent 99ab426 commit d7833d8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom-definitions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [LinkSet](src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md)
- [ImageSet](src/main/java/com/merkle/oss/magnolia/definition/custom/imageset/README.md)
- [VideoSet](src/main/java/com/merkle/oss/magnolia/definition/custom/videoset/README.md)
- [ChildNodeWrapper](src/main/java/com/merkle/oss/magnolia/definition/custom/childnodewrapper/README.md)

## validators
- MimeType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.merkle.oss.magnolia.definition.custom.childnodewrapper;

import info.magnolia.ui.editor.EditorView;
import info.magnolia.ui.editor.FormDefinition;
import info.magnolia.ui.editor.FormView;
import info.magnolia.ui.editor.ItemProviderDefinition;
import info.magnolia.ui.editor.JcrChildNodeProviderDefinition;
import info.magnolia.ui.field.ConfiguredComplexPropertyDefinition;
import info.magnolia.ui.field.ConfiguredFieldDefinition;
import info.magnolia.ui.field.EditorPropertyDefinition;
import info.magnolia.ui.framework.layout.FieldLayoutDefinition;
import info.magnolia.ui.framework.layout.SingleFieldLayoutProducer;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.jcr.Node;

public class ChildNodeWrapper<T extends EditorPropertyDefinition> extends ConfiguredComplexPropertyDefinition<Node> implements FormDefinition<Node> {
private final JcrChildNodeProviderDefinition nodeProvider = new JcrChildNodeProviderDefinition();
private final String name;
private final T field;

public ChildNodeWrapper(final String nodeName, final T field) {
this.field = field;
this.name = nodeName + "." + field.getName();
this.nodeProvider.setSupportI18N(false);
this.nodeProvider.setNodeName(nodeName);
}

public T getField() {
return field;
}

@Override
public Class<? extends EditorView<Node>> getImplementationClass() {
return (Class) FormView.class;
}

@Override
public boolean isI18n() {
return field.isI18n();
}

@Override
public String getName() {
return name;
}

@Override
public ItemProviderDefinition<Node, Node> getItemProvider() {
return nodeProvider;
}

@Override
public List<EditorPropertyDefinition> getProperties() {
return List.of(field);
}

@Override
public FieldLayoutDefinition<SingleFieldLayoutProducer> getLayout() {
return new SingleFieldLayoutProducer.Definition();
}

public static List<EditorPropertyDefinition> wrap(final String nodeName, final ConfiguredFieldDefinition<?>... fields) {
return wrap(nodeName, Arrays.stream(fields)).collect(Collectors.toList());
}
public static List<EditorPropertyDefinition> wrap(final String nodeName, final List<ConfiguredFieldDefinition<?>> fields) {
return wrap(nodeName, fields.stream()).collect(Collectors.toList());
}

public static Stream<EditorPropertyDefinition> wrap(final String nodeName, final Stream<ConfiguredFieldDefinition<?>> fields) {
return fields.map(field -> new ChildNodeWrapper<>(nodeName, field));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ChildNodeWrapper
Allows to save fields in child nodes without being nested in a composite/switchable field.

<img alt="screenshot" src='assets/screenshot.png' width='500'>
<br>
<img alt="jcr-structure" src='assets/jcr_structure.png' width='1000'>

## Usage
### Dialog

```java
import info.magnolia.ui.field.EditorPropertyDefinition;
import info.magnolia.module.blossom.annotation.TabFactory;

import com.merkle.oss.magnolia.definition.custom.childnodewrapper.ChildNodeWrapper;

@TabFactory("someTab")
public List<EditorPropertyDefinition> someTab() {
return ChildNodeWrapper.wrap("teaser",
new TextFieldDefinitionBuilder().i18n().build("title"),
new TextFieldDefinitionBuilder().i18n().build("text"),
new AssetLinkDefinitionBuilder().build("image")
);
}
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d7833d8

Please sign in to comment.