Want to use Tags to create meta data | Community
Skip to main content
Level 3
June 23, 2021
Solved

Want to use Tags to create meta data

  • June 23, 2021
  • 4 replies
  • 4472 views

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'

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 BimmiSo

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

4 replies

Ritesh_Mittal
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 23, 2021

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-java-api-best-practices.html?lang=en

Level 3
June 23, 2021

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

BimmiSoAdobe EmployeeAccepted solution
Adobe Employee
June 23, 2021

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

Level 3
June 24, 2021
Hey, need a clarification, What do you mean by "yourservice". I want to get tags attached to the page.
shelly-goel
Adobe Employee
Adobe Employee
June 23, 2021

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

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

vmadala
Level 3
June 24, 2021
 
   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>

 

 

Level 3
June 24, 2021

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.