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

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
Level 4

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.

View solution in original post

4 Replies

Avatar

Level 8

@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
Level 4

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.