Skip to content

Instantly share code, notes, and snippets.

@ksurendra
Created October 21, 2021 22:45
Show Gist options
  • Save ksurendra/900b2c5ce943fd85112e4de376aaaf20 to your computer and use it in GitHub Desktop.
Save ksurendra/900b2c5ce943fd85112e4de376aaaf20 to your computer and use it in GitHub Desktop.
AEM - Read Tags and Children
<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"/>
...
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;
}
}
}
<!-- Compoentn primary html -->
<nav data-sly-use.model="com.web.models.component.Navi"
data-sly-use.template="core/wcm/components/commons/v1/templates.html"
id="${navi.id}"
class="cmp-navi"
data-cmp-data-layer="${navi.data.json}"
aria-label="${navi.accessibilityLabel}">
<!-- Temporarily displaying json of the tags selected to be shown -->
<div>
<p>${model.naviTagsJson}</p>
</div>
</nav>
<sly data-sly-call="${template.placeholder @ isEmpty=!hasContent, classAppend='cmp-navigation'}"></sly>
package com.web.models.component;
..
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
...
@Model(adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL,
resourceType = EventList.RESOURCE_TYPE)
@Exporter(name = "jackson", extensions = "json")
public class Navi extends ComponentSlingModel {
...
private static final Gson GSON = new GsonBuilder().disableInnerClassSerialization().excludeFieldsWithoutExposeAnnotation().create();
...
@Inject
@Named("naviTags")
private String[] naviTags;
...
private List<String> naviTagsList = new ArrayList<>();
...
@PostConstruct
public void postConstruct() throws Exception {
if (naviTags!=null && naviTags.length > 0) {
naviTagsList.addAll(Arrays.asList(naviTags));
Iterator<String> navIterator = naviTagsList.iterator();
while (navIterator.hasNext()) {
String tagName = navIterator.next();
Tag userTag = tagManager.resolve(tagName);
if (userTag != null) {
AemTag aemTag = new AemTag(userTag, getResourceResolver(), locale, 0);
if (aemTag != null) {
tagsWithChildren.put(aemTag.getTitle(), getChildren(aemTag, userTag, locale));
}
}
}
}
....
} // End postConstruct
private List<AemTag> getChildren(final AemTag aemTag, final Tag userTag, final Locale locale) {
Resource tagResource = getResourceResolver().resolve(userTag.getPath());
List<AemTag> children = StreamSupport.stream(tagResource.getChildren().spliterator(), false)
.map(r -> r.adaptTo(Tag.class))
.filter(Objects::nonNull)
.map(t -> new AemTag(t, getResourceResolver(), locale, 0))
.collect(Collectors.toList());
return children;
}
public String getNaviTagsJson() {
return GSON.toJson(getTagsWithChildren());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment