I want to get list of tags set on resource in custom properties (not in cq:tags).
Values in these properties are set using tagpicker.
I tried tagManager.getTagsForSubtree(resource, false) but it's returning values only from cq:tags and tagManager.getTags(resource) returns no tags.
Can anyone help in this regards?
Thanks
Ritesh
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi,
If it is about custom property on the resource, then it is an usual way of retrieving properties from a resource which is via ValueMap.
Then the value retrieved can be resolved as a tag object.
ValueMap rescProps = resource.getValueMap();
String tagStr = rescProps.get("custom propertyname", String.class);
if(null != tagStr){
Tag actualTag = tagManager.resolve(tagStr);
}
Hi,
If it is about custom property on the resource, then it is an usual way of retrieving properties from a resource which is via ValueMap.
Then the value retrieved can be resolved as a tag object.
ValueMap rescProps = resource.getValueMap();
String tagStr = rescProps.get("custom propertyname", String.class);
if(null != tagStr){
Tag actualTag = tagManager.resolve(tagStr);
}
Thanks for reply.
However there are multiple custom properties set on a page with tag values. And those can change in future. I don't want to bind the functionality to particular property.
Is there any way to get those tags dynamically with above approach.
Thanks,
Ritesh
Views
Replies
Total Likes
Hi @rp5352629,
On a high level, if it shouldn't be tied to a specific property name, it will involve iteration.
But before I could comment about the apt solution, would like to know exact functionality that you are trying to achieve on the whole, if is fine to share.
Views
Replies
Total Likes
While using Sling Models, you can utilise the injector specific annotations.
Touch UI Dialogue:
<customTags
jcr:primaryType="nt:unstructured"
sling:resourceType="cq/gui/components/common/tagspicker"
fieldLabel="Custom Tags"
mode="contains"
name="./customTags"/>
Sling Model Java Class:
package com.myexamples.models;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import lombok.Getter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
@Model(adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ExampleSlingModel {
@SlingObject
private ResourceResolver resourceResolver;
@Getter
@ValueMapValue
private String[] customTags;
@Getter
private List<Tag> tags = new ArrayList<>();
@PostConstruct
private void init() {
resolveTags();
}
private void resolveTags() {
if (customTags != null) {
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
for (int i = 0, n = customTags.length; i < n; i++) {
Tag tag = tagManager.resolve(customTags[i]);
if (tag != null) {
tags.add(tag);
}
}
}
}
}
Sightly:
<sly data-sly-use.example="com.myexamplels.model.ExampleSlingModel"></sly>
<ul data-sly-list.tag="${example.tags}">
<li>${tag.path}, ${tag.tagID}, ${tag.name}</li>
</ul>
Tip: The code above assumes that the current user has view access to the cq:Tags. You can use service-users to obtain the TagManager if permissions are not granted to specific users.
Never forget to write JUNIT Tests for your Java Sling Models: https://sourcedcode.com/aem-sling-models-unit-test-junit-4-with-examples
I hope this helps.
Views
Replies
Total Likes
Hi @BrianKasingli ,
Hope it is addressed to the wrong recipient.
Hi @rp5352629,
Based on where exactly you are accessing the resource and hence its properties, the approach differs.
(In case of Sling Models which is via injecting the properties, Servlet/OSGI service, it is via Sling API or JCR API)
In any way, per the original question of getting property value as tag, it is via tagManager.resolve(String tagId) // tagId refers to the property value.
Views
Replies
Total Likes
Views
Likes
Replies