Get Tag data | Community
Skip to main content
Level 4
November 29, 2022
Solved

Get Tag data

  • November 29, 2022
  • 2 replies
  • 1033 views

I've a multifield where-in I can add multiple tags. Now what I'm trying is to fetch the title of the tag. Attached are the screenshots of the dialog.

 

Resource Type of the field is : /libs/cq/gui/components/coral/common/form/tagfield

 

 

This is the method with which I had tried - Problem is it throws null pointer exception.

 

private void getCourses(List<String> coursesType, List<String> coursesTypeList) { TagManager tagManager; Iterator<String> coursesubjectList = coursesType.iterator(); while (coursesubjectList.hasNext()) { resourceResolver = request.getResourceResolver(); tagManager = resourceResolver.adaptTo(TagManager.class); Tag tag = tagManager.resolve(coursesubjectList.next()); if(tag != null){ coursesTypeList.add(tag.getTitle()); } } }

 

Please suggest any corrections or you can even help me with the reqd code snippet.

 

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 SantoshSai

Hi @arindam6600 ,

Can you please check if your resourceResolver object is null?

Also refer this code: https://gist.github.com/ksurendra/900b2c5ce943fd85112e4de376aaaf20

Regards,

Santosh

2 replies

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
November 29, 2022

Hi @arindam6600 ,

Can you please check if your resourceResolver object is null?

Also refer this code: https://gist.github.com/ksurendra/900b2c5ce943fd85112e4de376aaaf20

Regards,

Santosh

Santosh Sai
Community Advisor
November 29, 2022

Hi @arindam6600 

I would suggest you to use tagpicker instead of using tagfield. It allows you to select multiple tags so you don't need to use multifield with tagfield.

sling:resourceType:cq/gui/components/common/tagspicker

Below code snippet should work

@Inject
private String[] tags;
private void getCourses(String[] tags, List<String> coursesTypeList) {
	resourceResolver = request.getResourceResolver();
	TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
	for (String tag: tags) {
		Tag tag = tagManager.resolve(tag);
		if(tag != null){
			coursesTypeList.add(tag.getTitle());
		}
	}
}