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'
Solved! Go to Solution.
Views
Replies
Total Likes
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
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
Hey @Ritesh_Mittal , when I'm using your steps it is showing that ValueMap cannot be resolved to a type. What should I do?
Views
Replies
Total Likes
Views
Replies
Total Likes
Hi @ShagunMalik ,
You need to import-
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.ResourceResolver;
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Views
Replies
Total Likes
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
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
@ShagunMalik You can also retrieve the tags directly in the htl as below
${pageProperties.cq:tags @ context='html'}
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>
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.
Views
Replies
Total Likes
@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;
Views
Replies
Total Likes
Hey @vmadala ,Still getting error "PostConstruct cannot be resolved to a type"
Views
Replies
Total Likes
@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();
}
}
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies