I have an asset in my DAM. I reference it as resource
with the following code:
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
Tag[] tags = tagManager.getTags(resource);
log.warn(tags.length + " tags found for resource:" + resource.getPath());
This dumps into the log that my asset has 0 tags. Yet when I look at this asset in the DAM admin, it shows it has several tags.
I am using the documentation here: https://docs.adobe.com/docs/en/cq/5-6-1/developing/tagging.html. Does anyone know if I'm doing this right?
The tags are stored in the metadata node of a DAM Asset (Refer Taggable Content in the AEM docs). Hence, you wouldn't be able to get the tags from the asset resource directly.
Instead, you can read the tags from the metadata of the asset as shown below.
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
Asset asset = resource.adaptTo(Asset.class);
Object[] tags = asset.getMetaData("cq:tags");
for (Object obj : tags) {
Tag tag = tagManager.resolve(obj.toString());
//Do something with your tag.
}