Created
October 21, 2021 22:45
-
-
Save ksurendra/900b2c5ce943fd85112e4de376aaaf20 to your computer and use it in GitHub Desktop.
AEM - Read Tags and Children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" | |
xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" | |
xmlns:granite="http://www.adobe.com/jcr/granite/1.0" | |
jcr:primaryType="nt:unstructured" | |
jcr:title="Faceted Navigation" | |
sling:resourceType="cq/gui/components/authoring/dialog"> | |
<content | |
jcr:primaryType="nt:unstructured" | |
sling:resourceType="granite/ui/components/coral/foundation/container" | |
granite:class="cmp-navigation__editor"> | |
... | |
<tags | |
jcr:primaryType="nt:unstructured" | |
sling:resourceType="cq/gui/components/coral/common/form/tagfield" | |
fieldLabel="Show Tags and Children" | |
fieldDescription="If set, the results will match the selected tags on Navigation component." | |
rootPath="/content/cq:tags" | |
multiple="{Boolean}true" | |
name="./naviTags"/> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.sling.api.resource.NonExistingResource; | |
.. | |
public class AemTag { | |
.. | |
@Expose | |
private final String id; | |
@Expose | |
private final String title; | |
public AemTag(final Tag tag, ResourceResolver resolver, Locale locale, int levels) throws IllegalArgumentException { | |
.. | |
Resource tagResource = resolver.resolve(tag.getPath()); | |
if (tagResource instanceof NonExistingResource) { | |
throw new IllegalArgumentException("Tag resource missing for " + tag.getPath()); | |
} | |
String tagTitle = tag.getTitle(); | |
if (levels == 0) { | |
this.id = tag.getTagID(); | |
this.title = tagTitle; | |
this.children = null; // Gson, by default, won't include null fields. Call serializeNulls() to change this. | |
} else { | |
List<AemTag> children = StreamSupport.stream(tagResource.getChildren().spliterator(), false) | |
.map(r -> r.adaptTo(Tag.class)) | |
.filter(Objects::nonNull) | |
.map(t -> new AemTag(t, resolver, locale, levels - 1)) | |
.collect(Collectors.toList()); | |
this.id = tag.getTagID(); | |
this.title = tagTitle; | |
this.children = children; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment