Want to use Tags to create meta data

Avatar

Avatar
Validate 1
Level 1
ShagunMalik
Level 1

Like

1 like

Total Posts

55 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
Ignite 1
Give Back
View profile

Avatar
Validate 1
Level 1
ShagunMalik
Level 1

Like

1 like

Total Posts

55 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
Ignite 1
Give Back
View profile
ShagunMalik
Level 1

23-06-2021

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'

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Contributor
Level 4
Bimmi_Soi
Level 4

Likes

68 likes

Total Posts

76 posts

Correct reply

24 solutions
Top badges earned
Contributor
Applaud 5
Boost 50
Ignite 1
Affirm 10
View profile

Avatar
Contributor
Level 4
Bimmi_Soi
Level 4

Likes

68 likes

Total Posts

76 posts

Correct reply

24 solutions
Top badges earned
Contributor
Applaud 5
Boost 50
Ignite 1
Affirm 10
View profile
Bimmi_Soi
Level 4

23-06-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

Answers (3)

Answers (3)

Avatar

Avatar
Boost 5
Level 3
vmadala
Level 3

Likes

21 likes

Total Posts

28 posts

Correct reply

5 solutions
Top badges earned
Boost 5
Establish
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Boost 5
Level 3
vmadala
Level 3

Likes

21 likes

Total Posts

28 posts

Correct reply

5 solutions
Top badges earned
Boost 5
Establish
Boost 3
Boost 1
Affirm 1
View profile
vmadala
Level 3

23-06-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>

 

 

ShagunMalik

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.

vmadala

@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;

 

ShagunMalik

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

vmadala

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

}

}

Avatar

Avatar
Affirm 100
MVP
shelly-goel
MVP

Likes

246 likes

Total Posts

409 posts

Correct reply

105 solutions
Top badges earned
Affirm 100
Give Back 25
Ignite 3
Give Back 10
Validate 1
View profile

Avatar
Affirm 100
MVP
shelly-goel
MVP

Likes

246 likes

Total Posts

409 posts

Correct reply

105 solutions
Top badges earned
Affirm 100
Give Back 25
Ignite 3
Give Back 10
Validate 1
View profile
shelly-goel
MVP

23-06-2021

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

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

Avatar

Avatar
Boost 5
Level 7
Ritesh_M
Level 7

Likes

179 likes

Total Posts

142 posts

Correct reply

44 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile

Avatar
Boost 5
Level 7
Ritesh_M
Level 7

Likes

179 likes

Total Posts

142 posts

Correct reply

44 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile
Ritesh_M
Level 7

23-06-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-j...