Get Tags Set on Resource in custom properties

riteshpatil

09-04-2020

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

AEM 6.4 Tag Management tagpicker tags

Accepted Solutions (1)

Accepted Solutions (1)

Vijayalakshmi_S

MVP

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

}

 

 

Answers (1)

Answers (1)

BrianKasingli

MVP

10-04-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.