Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Want to use Tags to create meta data

Avatar

Level 3

I need to write a Pojo class to get the value of cq:tags and then create meta data to get displayed as :

meta Name='tag_name' content='xyz'

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "yourservice");
try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(param)) {

Resource res = resolver.getResource("resource path");

ValueMap pageProperties = resource.getValueMap();

String tags = properties.get("cq:tag", String[].class);

TagManager tagManager = resolver.adaptTo(TagManager.class);

List<String> tagValue = new ArrayList<>();

if (Objects.nonNull(tagManager)) {
for (String v : tags) {
Tag tag = tagManager.resolve(v);
if (Objects.nonNull(tag)) {
tagValue.add(tag.getTitle());
}
}

}

 

iterate over the tagValue in sightly and populate your meta data

View solution in original post

16 Replies

Avatar

Community Advisor

Hi @ShagunMalik ,

 

You can use Sling APIs for this. Below are the steps-


1. Get Resource Resolver in your Class.

resolver = resolverFactory.getServiceResourceResolver(param);

2. Get the resource from Resource Resolver

Resource resource = resourceResolver.getResource("PATH-of-Content");

3. Adapt it to ValueMap

ValueMap properties = resource.getValueMap();

4. Get the specific property from value map-

String tag = properties.get("cq:tag", "Default value");

5. Return the value via Sling Model Getter method.

5. Grab the property in HTL to set in the meta attribute.

 

You can refer -

https://www.youtube.com/watch?v=CatRHs0rt4o

https://www.youtube.com/watch?v=LhvSNVagoZk

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/understand-j...

Avatar

Level 3

Hey @Ritesh_Mittal , when I'm using your steps it is showing that ValueMap cannot be resolved to a type. What should I do?

Avatar

Level 3
and when I'm declaring resolver it is showing error that "resolver cannot be resolved to a variable"

Avatar

Community Advisor

Hi @ShagunMalik ,

 

You need to import-

 

import org.apache.sling.api.resource.ValueMap;

import org.apache.sling.api.resource.ResourceResolver;

Avatar

Level 3

Hey @Ritesh_Mittal , thank you for it but still there are some errors. When I'm using line "resolver = resolverFactory.getServiceResourceResolver(param);" it is showing error: "param cannot be resolved to a variable","resolver cannot be resolved to a variable" and "resolverFactory cannot be resolved". Please help.

Avatar

Correct answer by
Employee Advisor

Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "yourservice");
try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(param)) {

Resource res = resolver.getResource("resource path");

ValueMap pageProperties = resource.getValueMap();

String tags = properties.get("cq:tag", String[].class);

TagManager tagManager = resolver.adaptTo(TagManager.class);

List<String> tagValue = new ArrayList<>();

if (Objects.nonNull(tagManager)) {
for (String v : tags) {
Tag tag = tagManager.resolve(v);
if (Objects.nonNull(tag)) {
tagValue.add(tag.getTitle());
}
}

}

 

iterate over the tagValue in sightly and populate your meta data

Avatar

Level 3
Hey, need a clarification, What do you mean by "yourservice". I want to get tags attached to the page.

Avatar

Employee Advisor
subServiceName which you would have provided in your Apache Sling Service User Mapper Service

Avatar

Employee Advisor

@ShagunMalik  You can also retrieve the tags directly in the htl as below

${pageProperties.cq:tags @ context='html'}

Avatar

Level 4
 
   if I understand you correctly, you're trying to load the tags and want a modal to fetch these meta tags. Sample code for you:
  

 

1. Create meta tags in  under "/content/cq:tags/metatags"

2. Modal Java class:

 

@Model(adaptables=Resource.class, defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL)

public class MetaTagModel {

 

final String path = "/content/cq:tags/metatags";

 

@SlingObject

private ResourceResolver resourceResolver;

 

Tag tag;

 

@PostConstruct

protectedvoid init() {

TagManager tagManager = resourceResolver.adaptTo(TagManager.class);

tag = tagManager.resolve(path);

}

 

public Iterator<Tag> getTags() {

return tag.listChildren();

}

 

3. Use the below code in the header.vtl to render the meta tags

 

<sly data-sly-use.metaTag="com.eqx.aem.eqxcorp.models.MetaTagModel">
<sly data-sly-list="${metaTag.tags}">
<meta name="${item.name}" content="${item.title}"/>
</sly>
</sly>

 

 

Avatar

Level 3

Hey @vmadala , I'm getting multiple errors. List is :

"defaultInjectionStrategy cannot be resolved to a variable" ,

"Model cannot be resolved to a type",

"SlingObject cannot be resolved to a type",

"Tag cannot be resolved to a type",

"PostConstruct cannot be resolved to a type",

"TagManager cannot be resolved to a type",

"Iterator cannot be resolved to a type" Please help.

Avatar

Level 4

@ShagunMalik, Please import the below classes into the class,

 

 

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.injectorspecific.SlingObject;

 

import com.day.cq.tagging.Tag;

import com.day.cq.tagging.TagManager;

 

import java.util.Iterator;

 

import org.apache.sling.models.annotations.DefaultInjectionStrategy;

 

Avatar

Level 3

Hey @vmadala ,Still getting error "PostConstruct cannot be resolved to a type"

Avatar

Level 4

@ShagunMalikMy bad missed one import

 

import javax.annotation.PostConstruct;

 

Here you go complete file,

 

import javax.annotation.PostConstruct;

 

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.injectorspecific.SlingObject;

 

import com.day.cq.tagging.Tag;

import com.day.cq.tagging.TagManager;

 

import java.util.Iterator;

 

import org.apache.sling.models.annotations.DefaultInjectionStrategy;

 

@Model(adaptables=Resource.class, defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL)

public class MetaTagAPI {

 

final String path = "/content/cq:tags/metatags";

 

@SlingObject

private ResourceResolver resourceResolver;

 

Tag tag;

 

@PostConstruct

protectedvoid init() {

TagManager tagManager = resourceResolver.adaptTo(TagManager.class);

tag = tagManager.resolve(path);

}

 

public Iterator<Tag> getTags() {

return tag.listChildren();

}

}