Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Get Tag data

Avatar

Level 5

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

 

arindam6600_0-1669738555205.png

 

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.

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @arindam6600 ,

Can you please check if your resourceResolver object is null?

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

Regards,

Santosh

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @arindam6600 ,

Can you please check if your resourceResolver object is null?

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

Regards,

Santosh

Avatar

Level 6

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

salamswapnil_0-1669742579111.png

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());
		}
	}
}