How to retrive or display added cq:tags on the page using tag manager | Community
Skip to main content
Level 4
June 21, 2022
Solved

How to retrive or display added cq:tags on the page using tag manager

  • June 21, 2022
  • 4 replies
  • 853 views

Hi all, 

I have created one custom tab under properties and given tagfield to let the author to select tags. Now I want to display those selected tags in the page. How can I do that ??

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Shiv_Prakash_Patel

You should use InheritanceValueMap to get page properties in Sling Model and Use Tag Manager to Resolve cq:tags.

 

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TagReadingFromPageProperties {

@Inject
private InheritanceValueMap pageProperties;

@Self
private SlingHttpServletRequest request;

Map<String, String> tagMap;

@PostConstruct
protected void init() {
String[] allTags = pageProperties.get("cq:tags", String[].class);
TagManager tagManager = request.getResourceResolver().adaptTo(TagManager.class);
tagMap = new HashMap<>();

for (String tags : allTags) {
Tag tag = tagManager.resolve(tags);
tagMap.put(tag.getName(), tag.getTitle());
}
}

public Map<String, String> getTagMap() {
return tagMap;
}
}

You can iterate tagMap to get list through sightly.

4 replies

Adobe Employee
June 21, 2022

You should resolve a tag via TagManager (https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/index.html?com/day/cq/tagging/TagManager.html)

 

 resourceResolver.adaptTo(TagManager.class)

ShaileshBassi
Community Advisor
Community Advisor
June 21, 2022

@tessa_learner1 It should be straight forward:

 

 

@Model(adaptable=Resource.class)

public class myClass {

@Self

Resource resource;
List<String> tagNames;
@PostConstruct

public void init() {

  	PageManager pm = resource.getResourceResolver().adaptTo(PageManager.class);
  	Page containingPage = pm.getContainingPage (resource);
  	ValueMap pageProperties = containingPage.getProperties();
	String[] tags = pageProperties.get("cq:tags", new String[0]);
tagNames = new ArrayList<>(); TagManager tagManager = resourceResolver.adaptTo(TagManager.class); for(String tag : tags){ Tag tag = tagManager.resolve(tag);
tagNames.add(tag.getTitle); } } }

 

You can display the list using sightly in the component.

 

Thanks

Himanshu_Jain
Community Advisor
Community Advisor
June 21, 2022
Shiv_Prakash_Patel
Community Advisor
Shiv_Prakash_PatelCommunity AdvisorAccepted solution
Community Advisor
June 21, 2022

You should use InheritanceValueMap to get page properties in Sling Model and Use Tag Manager to Resolve cq:tags.

 

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TagReadingFromPageProperties {

@Inject
private InheritanceValueMap pageProperties;

@Self
private SlingHttpServletRequest request;

Map<String, String> tagMap;

@PostConstruct
protected void init() {
String[] allTags = pageProperties.get("cq:tags", String[].class);
TagManager tagManager = request.getResourceResolver().adaptTo(TagManager.class);
tagMap = new HashMap<>();

for (String tags : allTags) {
Tag tag = tagManager.resolve(tags);
tagMap.put(tag.getName(), tag.getTitle());
}
}

public Map<String, String> getTagMap() {
return tagMap;
}
}

You can iterate tagMap to get list through sightly.

Shiv Prakash