How to read tag titles for tags entered in tagfield? | Community
Skip to main content
Level 3
November 4, 2022
Solved

How to read tag titles for tags entered in tagfield?

  • November 4, 2022
  • 1 reply
  • 1159 views

I have a tagfield included in my dialog. I want to print the titles for the tags being added in tagfield how can I achieve that? A code snippet for the same will be really useful.

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

Hi @manasi29 ,

Here is a code snippet of reading a tag from the page properties dialog, Similarly you can modify the code for the custom component dialog as well in the sling model.

//Getting all Tags from InheritanceValueMap
String[] allTags = pageProperties.get("cq:Tags", String[].class);
TagManager tagManager = request.getResourceResolver().adaptTo(TagManager.class);
for (String tags : allTags) {
Tag tag = tagManager.resolve(tags);
String tagTitle = tag.getTitle();
String tagName = tag.getName();
//Custom Code
}
  •  In the above snippet, referencing the InheritanceValueMap and getting resource resolver from request. You can also take resource resolver with System User.
  • If you have a single tag you can directly resolve it

 

Hope this could help you !!!

Regards

Shiv

1 reply

Shiv_Prakash_Patel
Community Advisor
Shiv_Prakash_PatelCommunity AdvisorAccepted solution
Community Advisor
November 4, 2022

Hi @manasi29 ,

Here is a code snippet of reading a tag from the page properties dialog, Similarly you can modify the code for the custom component dialog as well in the sling model.

//Getting all Tags from InheritanceValueMap
String[] allTags = pageProperties.get("cq:Tags", String[].class);
TagManager tagManager = request.getResourceResolver().adaptTo(TagManager.class);
for (String tags : allTags) {
Tag tag = tagManager.resolve(tags);
String tagTitle = tag.getTitle();
String tagName = tag.getName();
//Custom Code
}
  •  In the above snippet, referencing the InheritanceValueMap and getting resource resolver from request. You can also take resource resolver with System User.
  • If you have a single tag you can directly resolve it

 

Hope this could help you !!!

Regards

Shiv

Shiv Prakash
Manasi29Author
Level 3
November 4, 2022

Thanks a lot.