Get Tags Set on Resource in custom properties | Community
Skip to main content
New Participant
April 9, 2020
Solved

Get Tags Set on Resource in custom properties

  • April 9, 2020
  • 2 replies
  • 8763 views

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

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 Vijayalakshmi_S

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

}

 

 

2 replies

BrianKasingli
New Participant
April 10, 2020

@vijayalakshmi_s,

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.

Vijayalakshmi_S
New Participant
April 10, 2020

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. 

 

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
New Participant
April 9, 2020

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

}

 

 

rp5352629Author
New Participant
April 13, 2020

@vijayalakshmi_s,

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