Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

Level 5

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 ??

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

4 Replies

Avatar

Community Advisor

@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

Avatar

Correct answer by
Community Advisor

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