Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

TagManager resolve not working

Avatar

Level 4

Hi,

 

I am trying to create a filter component - to filter based on tags. The idea is to pass tags from a specific path to front, so user can choose from dropdown.

I am having an issue, with Tag Manager methods, like resolve.

Code below:

 @Override
    public String getAllTags(){
        Tag tag = null;
        TagManager tagManager = null;
    
        tagManager =  resourceResolver.adaptTo(TagManager.class);
        tag = tagManager.resolveByTitle("Region");
      

        String name = tagManager.getNamespaces()[3].getTitle();
        return tag.getPath();
    }

 

This is just a test method. I know that the return type should be List<Tag>, but for now I just want to get a specific tag path, to understand hot this works. But it is returning null. I know most of the times this issue is related to user permissions, but I already updated the permission to read for content/cq:tags. Thanks!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @AD-Engineer 

 

Please follow the below approach:

 

Create a VO/POJO class with all the desired fields. I added only 2 for DEMO.

public class TagVO {
private String tagName;
private String tagTitle;

public String getTagName() {
return tagName;
}

public void setTagName(String tagName) {
this.tagName = tagName;
}

public String getTagTitle() {
return tagTitle;
}

public void setTagTitle(String tagTitle) {
this.tagTitle = tagTitle;
}
}

 

 Create a Model class and call it in component:

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TestModel {

private static final Logger log = LoggerFactory.getLogger(TestModel.class);

@SlingObject
private ResourceResolver resourceResolver;

private List<TagVO> tagList;

@PostConstruct
protected void init() {
log.info("***** :: Start :: *****");
tagList = new ArrayList<>();
String tagPath = "/content/cq:tags/brands/whatever"; // Here you will pass the parent path to all tags. Ensure anonumous user access is there for this path.
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
if (tagManager != null) {
Tag tagFromPath = tagManager.resolve(tagPath);
Iterator<Tag> tagIterator = tagFromPath.listChildren();
tagIterator.forEachRemaining(tag -> {
TagVO tagVO = new TagVO();
tagVO.setTagName(tag.getName());
tagVO.setTagTitle(tag.getTitle());
tagList.add(tagVO);
});
}
log.info("***** :: End :: *****");
}

public List<TagVO> getTagList() {
return tagList;
}
}

 

HTL:

<sly data-sly-use.menuobj="com.aem.core.models.TestModel">
<sly data-sly-list.listObj="${menuobj.tagList}">
<li value="${listObj.tagName}">${listObj.tagTitle}</li>
</sly>
</sly>

HTL script can be modified to show as dropdown. I just added as simple list item.

 

Hope this helps!

Thanks 

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @AD-Engineer 

 

Please follow the below approach:

 

Create a VO/POJO class with all the desired fields. I added only 2 for DEMO.

public class TagVO {
private String tagName;
private String tagTitle;

public String getTagName() {
return tagName;
}

public void setTagName(String tagName) {
this.tagName = tagName;
}

public String getTagTitle() {
return tagTitle;
}

public void setTagTitle(String tagTitle) {
this.tagTitle = tagTitle;
}
}

 

 Create a Model class and call it in component:

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TestModel {

private static final Logger log = LoggerFactory.getLogger(TestModel.class);

@SlingObject
private ResourceResolver resourceResolver;

private List<TagVO> tagList;

@PostConstruct
protected void init() {
log.info("***** :: Start :: *****");
tagList = new ArrayList<>();
String tagPath = "/content/cq:tags/brands/whatever"; // Here you will pass the parent path to all tags. Ensure anonumous user access is there for this path.
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
if (tagManager != null) {
Tag tagFromPath = tagManager.resolve(tagPath);
Iterator<Tag> tagIterator = tagFromPath.listChildren();
tagIterator.forEachRemaining(tag -> {
TagVO tagVO = new TagVO();
tagVO.setTagName(tag.getName());
tagVO.setTagTitle(tag.getTitle());
tagList.add(tagVO);
});
}
log.info("***** :: End :: *****");
}

public List<TagVO> getTagList() {
return tagList;
}
}

 

HTL:

<sly data-sly-use.menuobj="com.aem.core.models.TestModel">
<sly data-sly-list.listObj="${menuobj.tagList}">
<li value="${listObj.tagName}">${listObj.tagTitle}</li>
</sly>
</sly>

HTL script can be modified to show as dropdown. I just added as simple list item.

 

Hope this helps!

Thanks